import java.util.HashMap;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
HashMap<Student,String> map = new HashMap<>();
map.put(new Student("小王",10),"北京");
map.put(new Student("小李",11),"广州");
map.put(new Student("小张",12),"重庆");
map.put(new Student("小明",13),"上海");
map.put(new Student("小明",13),"深圳");
Set<Student> key = map.keySet();
for (Student s : key) {
String value = map.get(s);
System.out.println(s + value);
}
}
}
import java.util.Objects;
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return
"name='" + name + '\'' +
", age=" + age
;
}
}