HashMap集合_String值是Student
案例:HashMap集合存储学生对象并遍历 需求:创建一个HashMap集合,键是学号(String类型),值是学生对象(Student类型)。存储三个键值对元素,并遍历
思路: 1、定义学生类 2、创建HashMap集合对象 3、创建学生对象 4、把学生添加到集合。注意集合的值是学生,集合的键是String类型 5、遍历集合。上两节课学了两种Map集合的遍历方式 (1)方式1:键找值 (1)方式2:键值对对象找键和值
HashMap集合_String值是Student的练习
xxxxxxxxxxpackage ch17;
public class a_6_1Student {
//成员变量 private String name; private int age;
//无参构造方法 public a_6_1Student() { }
//无参构造方法 public a_6_1Student(String name, int age) { this.name = name; this.age = age; }
//成员变量对应的get、set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
}xxxxxxxxxxpackage ch17;
import java.util.HashMap;import java.util.Map;import java.util.Set;
public class a_6_2测试 {
public static void main(String[] args) {
//创建HashMap集合对象 HashMap<String, a_6_1Student> hm = new HashMap<String, a_6_1Student>();
//创建学生对象 a_6_1Student s1 = new a_6_1Student("张三", 18); a_6_1Student s2 = new a_6_1Student("李四", 19); a_6_1Student s3 = new a_6_1Student("王五", 20);
//把学生添加到集合。注意集合的值是学生,集合的键是String类型 hm.put("hello1", s1); hm.put("hello2", s2); hm.put("hello3", s3);
//遍历集合。方式1:键找值 Set<String> keySet = hm.keySet(); //遍历键keySet的集合 for (String key : keySet) { a_6_1Student value = hm.get(key); //键找值 System.out.println(key + "," + value.getName() + "," + value.getAge()); } System.out.println("---------------------------");
//----------------------------------------------------------------------------------------------------------------
//遍历集合。方式2:键值对对象找键和值 Set<Map.Entry<String, a_6_1Student>> entrySet = hm.entrySet(); //快捷键:先输入hm.entrySet();,再按Ctrl+Alt+V for (Map.Entry<String, a_6_1Student> me : entrySet) { String key = me.getKey(); a_6_1Student value = me.getValue(); System.out.println(key + "," + value.getName() + "," + value.getAge()); }
}}
HashMap集合_Student值是String
需求:创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对元素,并遍历。 要求:保证键的唯一性:即如果学生对象的成员变量值相同,我们就认为是同一个对象
难点主要是上面的要求,例如怎么保证键的唯一性,这个键其实就是学生对象,也就是如何保证我们自定义的对象的键的唯一性 保证键的唯一性思路:该集合是HashMap集合,即底层的数据结构是哈希表,所以我们只要在学生类里面重写两个方法就可以了 难点总结:学生作为键,还有保证键的唯一性
思路: 1、定义学生类 2、创建HashMap集合对象 3、创建学生对象 4、把学生添加到集合。注意:这里的学生是作为键存在的 5、遍历集合 6、在学生类中出现两个方法。hashCode()和equals()
HashMap集合_Student值是String的练习
xxxxxxxxxxpackage ch17;
public class a_7_1Student {
//成员变量 private String name; private int age;
//无参构造方法 public a_7_1Student() { }
//无参构造方法 public a_7_1Student(String name, int age) { this.name = name; this.age = age; }
//成员变量对应的get、set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
//重写hashCode()和equals()方法。快捷键:Alt+insert,再点击equals() and hashCode(),再一直点击Next public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;
a_7_1Student that = (a_7_1Student) o;
if (age != that.age) return false; return name != null ? name.equals(that.name) : that.name == null; } public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; }
}xxxxxxxxxxpackage ch17;
import java.util.HashMap;import java.util.Set;
public class a_7_2测试 {
public static void main(String[] args) {
//创建HashMap集合对象 HashMap<a_7_1Student, String> hm = new HashMap<a_7_1Student, String>(); //注意键是Student类型,值是String类型
//创建学生对象 a_7_1Student s1 = new a_7_1Student("张三", 18); a_7_1Student s2 = new a_7_1Student("李四", 19); a_7_1Student s3 = new a_7_1Student("王五", 20); a_7_1Student s4 = new a_7_1Student("王五", 20);//注意s3和s4的成员内容是一样的
//把学生添加到集合。注意:这里的学生是作为键存在的 hm.put(s1, "海南"); hm.put(s2, "厦门"); hm.put(s3, "福建"); hm.put(s4, "覆盖了吗");//因为上面s3和s4的成员内容是一样,即键一样,原来的s3是对应福建,s3里面的福建会被这行的"覆盖了吗"值覆盖掉吗 //我们需要它覆盖,但是却没有覆盖。解决:在学生类里面重写hashCode()和equals()方法。重写了之后,后面的值"覆盖了吗"就会覆盖"福建"
//遍历集合。有两种写法可以实现遍历HashMap集合,这里只使用键找值的写法 Set<a_7_1Student> keySet = hm.keySet(); //快捷键:先输入hm.keySet();,再按Ctrl+Alt+V //遍历该集合 for (a_7_1Student key : keySet) { //注意这里这个键是Student类型,遍历的时候要写Student类型 //键找值 String value = hm.get(key); //注意这个key是学生对象 System.out.println(key.getName() + "," + key.getAge() + "," + value); }
}}