1,HashMap 集合的 特点:

  1. 元素按照键不重复,无索引,存储和取出无顺序。(与Map体系一致)
  2. 集合中的:键为 key;值为 value;
  3. 两者都是泛型;

  4. 2,HashMap集合 的方法:

    1,创建HashMap集合:Set<集合名.Entry>

    1. Set<Map.Entry<String, String>> entrySet = map.entrySet();

    image.png

  5. 添加数据:put

    1. 当值不存在,则是添加;
    2. 当值存在时,再添加数据会修改原来的值(修改);
      1. mapA.put("da","dd");
  6. 通过键找值:get

    1. System.out.println(mapA.get("da"));
  7. 根据键删除对应的值:remove

    1. mapA.remove("da");
  8. 移除所有键值对的元素:clear

    1. mapA.clear();
  9. 判断集合是否包含指定的键:containKey

    1. boolean da = mapA.containsKey("da");
  10. 判断集合是否包含指定的值:containValue

    1. boolean dd = mapA.containsValue("dd");
  11. 判断是否为空:isEmpty

    1. boolean empty = mapA.isEmpty();
  12. 集合长度:size

    1. mapA.size();

    3,HashMap的遍历方式一:键找值:keySet()

    仅通过键来遍历; ```java public class M { public static void main(String[] args) { HashMap mapA = new HashMap<>();

    mapA.put(“da”,”dd”); mapA.put(“dda”,”Gd”);

    //利用set集合的特性将值存在set集合达到不重复的目的,再通过遍历set集合到达键找值; Set setKeys = mapA.keySet(); //遍历set的集合 for (String key : setKeys) {

    1. String keyA=mapA.get(key);
    2. System.out.println(keyA);

    } }

    //重写toSring的输出方法: public String toString() { return “M{ }”; }

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25975946/1644824776544-c4e1d940-b651-46ba-8fb4-f7ed066df570.png#clientId=u3989c462-d18e-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=686&id=u316ad15f&name=image.png&originHeight=686&originWidth=694&originalType=binary&ratio=1&rotation=0&showTitle=false&size=325574&status=done&style=none&taskId=u46a6da7e-00b2-4bed-9097-a66f8d13059&title=&width=694)
  2. <a name="waWBr"></a>
  3. ## 4,HashMap的遍历方式二:键值对:entrySet()
  4. 1. 通过键和值同时遍历;键和值就存在一个Entry里面;而Entry存放在set集合中;**(一个键对应的一个值就是一个entry对象)**
  5. 1. 键值对的遍历流程:
  6. 1. 获取所有Entry对象;
  7. 1. 遍历每个取出的Entry
  8. 1. 取出Entry的键和值;
  9. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25975946/1644825626749-74ad1260-0d85-4eaa-98b9-b804dd35a0df.png#clientId=u3989c462-d18e-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=302&id=u6ad75701&name=image.png&originHeight=302&originWidth=913&originalType=binary&ratio=1&rotation=0&showTitle=false&size=128625&status=done&style=none&taskId=u44d69284-9526-4fd6-97a4-72379318e7f&title=&width=913)
  10. ```java
  11. //获取entry
  12. Set<Map.Entry<String, String>> setKeys = mapA.entrySet();
  13. //遍历取出的entry
  14. for (Map.Entry<String, String> entry : setKeys) {
  15. //取出entry里面的键和值:
  16. String key = entry.getKey();
  17. String value = entry.getValue();
  18. System.out.println(key + " + " + value);
  19. }

5,HashMap集合储存自定义对象并遍历:

image.png

  1. 去重就要去重写equals和hashcode方法,保证键的唯一性; ```java // //Student类: package day07_KeTang.Maps.Demo03;

/**

  • @author Jztice5
  • @date 2022年02月14日 16:43 */

public class Student { private String name; private int age; private String address;

  1. public Student() {
  2. }
  3. public Student(String name, int age) {
  4. this.name = name;
  5. this.age = age;
  6. }
  7. public Student(String name, int age, String address) {
  8. this.name = name;
  9. this.age = age;
  10. this.address = address;
  11. }
  12. /**
  13. * 获取
  14. * @return name
  15. */
  16. public String getName() {
  17. return name;
  18. }
  19. /**
  20. * 设置
  21. * @param name
  22. */
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. /**
  27. * 获取
  28. * @return age
  29. */
  30. public int getAge() {
  31. return age;
  32. }
  33. /**
  34. * 设置
  35. * @param age
  36. */
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40. public String toString() {
  41. return "Student{name = " + name + ", age = " + age + "}";
  42. }
  43. /**
  44. * 获取
  45. * @return address
  46. */
  47. public String getAddress() {
  48. return address;
  49. }
  50. /**
  51. * 设置
  52. * @param address
  53. */
  54. public void setAddress(String address) {
  55. this.address = address;
  56. }
  57. //重写达到去重的目的:
  58. @Override
  59. public boolean equals(Object o) {
  60. if (this == o) return true;
  61. if (o == null || getClass() != o.getClass()) return false;
  62. Student student = (Student) o;
  63. if (age != student.age) return false;
  64. if (name != null ? !name.equals(student.name) : student.name != null) return false;
  65. return address != null ? address.equals(student.address) : student.address == null;
  66. }
  67. @Override
  68. public int hashCode() {
  69. int result = name != null ? name.hashCode() : 0;
  70. result = 31 * result + age;
  71. result = 31 * result + (address != null ? address.hashCode() : 0);
  72. return result;
  73. }

}

// //Text类: package day07_KeTang.Maps.Demo04;

import java.util.HashMap; import java.util.Map; import java.util.Set;

/**

  • @author Jztice5
  • @date 2022年02月14日 16:57 */

public class Text { public static void main(String[] args) { //创建Hashmap集合 mapA HashMap mapA = new HashMap<>();

  1. //创建学生对象并赋值;
  2. Student s1 = new Student("da", 19);
  3. Student s2 = new Student("da", 19);
  4. Student s3 = new Student("da", 19);
  5. //添加对象到集合
  6. mapA.put(s1, "gz");
  7. mapA.put(s2, "cq");
  8. mapA.put(s3, "sc");
  9. //获取mapA集合的entry对象 ;
  10. Set<Map.Entry<Student, String>> entries = mapA.entrySet();
  11. //遍历所有取出的entru对象
  12. for (Map.Entry<Student, String> entry : entries) {
  13. //将entry对象中的键和值遍历取出
  14. Student key = entry.getKey();
  15. String value = entry.getValue();
  16. //out
  17. System.out.println(key + ":" + value);
  18. }
  19. }

}

```