JavaSE-16.1.2【案例-HashMap集合存储学生对象并遍历*2】
1 package day7.lesson1.anli1; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /* 8 1.5 Map集合的案例 9 10 1.5.1 案例-HashMap集合存储学生对象并遍历#1 11 创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素,并遍历 12 */ 13 public class HashMapDemo { 14 public static void main(String[] args) { 15 HashMap<String, Student> map = new HashMap<>(); 16 17 Student s1 = new Student("zhangsan" , 12); 18 Student s2 = new Student("lisi" , 14); 19 Student s3 = new Student("wangwu" , 12); 20 21 map.put("itheima01", s1); 22 map.put("itheima02", s2); 23 map.put("itheima03", s3); 24 25 Set<String> keySet = map.keySet(); 26 for (String id: keySet){ 27 Student stu = map.get(id); 28 System.out.println("学号:" + id + ", 姓名:" + stu.getName() + ", 年龄:" + stu.getAge()); 29 } 30 /* 31 学号:itheima01, 姓名:zhangsan, 年龄:12 32 学号:itheima02, 姓名:lisi, 年龄:14 33 学号:itheima03, 姓名:wangwu, 年龄:12 34 */ 35 36 System.out.println(); 37 Set<Map.Entry<String, Student>> entrySet = map.entrySet(); 38 for (Map.Entry<String, Student> me: entrySet){ 39 String id = me.getKey(); 40 Student stu = me.getValue(); 41 System.out.println("学号:" + id + ", 姓名:" + stu.getName() + ", 年龄:" + stu.getAge()); 42 } 43 } 44 }
1 package day7.lesson1.anli1; 2 3 public class Student { 4 5 private String name; 6 private int age; 7 8 public Student() { 9 } 10 11 public Student(String name, int age) { 12 this.name = name; 13 this.age = age; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public void setAge(int age) { 21 this.age = age; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public int getAge() { 29 return age; 30 } 31 }
1 package day7.lesson1.anli2; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /* 8 1.5.2 案例-HashMap集合存储学生对象并遍历#2 9 创建一个HashMap集合,键是学生对象(Student),值是居住地 (String)。存储多个元素,并遍历。 10 要求保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象 11 学生类中重写hashCode() & equels() 12 */ 13 public class HashMapDemo { 14 public static void main(String[] args) { 15 HashMap<Student, String> hm = new HashMap<>(); 16 17 Student s1 = new Student("zhangsan", 12); 18 Student s2 = new Student("lisi", 14); 19 Student s3 = new Student("wangwu", 12); 20 Student s4 = new Student("wangwu", 12); 21 22 hm.put(s1, "北京"); 23 hm.put(s2, "上海"); 24 hm.put(s3, "南京"); 25 hm.put(s4, "武汉"); 26 27 Set<Student> keySet = hm.keySet(); 28 for (Student student: keySet){ 29 String address = hm.get(student); 30 System.out.println("姓名:" + student.getName() + ", 年龄:" + student.getAge() + ", 地址:" + address); 31 } 32 /* 33 //学生类中重写hashCode() & equels()之前: 34 姓名:lisi, 年龄:14, 地址:上海 35 姓名:wangwu, 年龄:12, 地址:武汉 36 姓名:zhangsan, 年龄:12, 地址:北京 37 姓名:wangwu, 年龄:12, 地址:南京 38 39 //学生类中重写hashCode() & equels()之后: 40 姓名:zhangsan, 年龄:12, 地址:北京 41 姓名:wangwu, 年龄:12, 地址:武汉 //值(address)被覆盖了 42 姓名:lisi, 年龄:14, 地址:上海 43 */ 44 45 System.out.println(); 46 Set<Map.Entry<Student, String>> entrySet = hm.entrySet(); 47 for (Map.Entry<Student, String> me: entrySet){ 48 Student student = me.getKey(); 49 String address = me.getValue(); 50 System.out.println("姓名:" + student.getName() + ", 年龄:" + student.getAge() + ", 地址:" + address); 51 } 52 } 53 }
1 package day7.lesson1.anli2; 2 3 public class Student { 4 5 private String name; 6 private int age; 7 8 public Student() { 9 } 10 11 public Student(String name, int age) { 12 this.name = name; 13 this.age = age; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public void setAge(int age) { 21 this.age = age; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public int getAge() { 29 return age; 30 } 31 32 @Override 33 public boolean equals(Object o) { 34 if (this == o) return true; 35 if (o == null || getClass() != o.getClass()) return false; 36 37 Student student = (Student) o; 38 39 if (age != student.age) return false; 40 return name != null ? name.equals(student.name) : student.name == null; 41 } 42 43 @Override 44 public int hashCode() { 45 int result = name != null ? name.hashCode() : 0; 46 result = 31 * result + age; 47 return result; 48 } 49 }