class a{
private int age;
public a(int age) {
this.age = age;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
a a = (a) o;
return age == a.age;
}
@Override
public int hashCode() {
return Objects.hash(age);
}
}
public class Main {
public static void main(String[] args) {
HashMap<a, Object> map = new HashMap<>();
a a = new a(20);
map.put(a, 123);
System.out.println(a.hashCode());
a.setAge(21);
map.put(a, 234);
System.out.println(a.hashCode());
Set<a> set = map.keySet();
for (a a1 : set) {
System.out.println("hashcode为:"+a1.getAge());
}
System.out.println("map的长度为:"+map.size());
}
}
hashmap的key重复
最新推荐文章于 2024-04-18 03:27:45 发布