public class Student {
private Integer id;
private String name;
//get和set省略
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id) &&
Objects.equals(name, student.name);
}
}
复制代码
Test
public class Test {
public static void main(String[] args) {
Student student1 = new Student(1,“小朱”);
Student student2 = new Student(2,“大牛”);
HashMap<Student,String> hashMap = new HashMap<>();
hashMap.put(student1,“菜鸡”);
hashMap.put(student2,“大神”);
//这个方法