


package com.itheima.d9_map_impl;import com.itheima.d1_set.Student;import java.util.HashMap;import java.util.Map;public class HashMapDemo1 { public static void main(String[] args) { // Map集合是根据键去除重复元素 Map<Student,String> maps = new HashMap<>(); // 创建三个学生对象放到 HashMap集合中 Student s1 = new Student("无恙",20,'男'); Student s2 = new Student("无恙",20,'男'); Student s3 = new Student("周雄",20,'男'); // 因为Student类里面重写了HashCode和equals方法,根据哈希值去重复 maps.put(s1,"北京"); // 这个时候形参一样s1和s2,被当成同一个对象,去重复了 maps.put(s2,"上海"); // s2,覆盖了s1 maps.put(s3,"广州"); System.out.println(maps); //{Student{name='无恙', age=20, sex=男}=上海, Student{name='周雄', age=20, sex=男}=广州} // 最终只会保留两个元素 }}