【发布时间】:2015-05-29 10:48:58
【问题描述】:
每当我尝试运行这个程序时,我都会得到以下输出:
6150
5000
1015612567
列表中的人:
{A 人=100,B 人=50}
下一步:
人物:人物 A 哈希码:507806258 值:100
下一步:
人物:人物 B 哈希码:507806309 值:50
String name;
int day;
static Map<String, Integer> people = new HashMap<>();
public PeopleHash(String name, int day) {
this.name = name;
this.day = day;
}
public String getName() {
return name;
}
public int getDay() {
return day;
}
public boolean equals(PeopleHash other) {
if (other instanceof PeopleHash) {
PeopleHash d = (PeopleHash)other;
return name == d.name && day == d.day;
} else {
return false;
}
}
@Override
public int hashCode() {
return 50 * day;
}
public PeopleHash peopleEntry(){
String namn = "A";
int schiffre = 123;
return null;
}
public static void main(String[] args) {
PeopleHash p1 = new PeopleHash("Isolde", 123);
PeopleHash p2 = new PeopleHash("Jean", 100);
people.put("Person A", 100);
people.put("Person B", 50);
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
System.out.println("People in list: \n" + people);
for (Entry<String, Integer> entry : people.entrySet()) {
String a = entry.getKey();
int b = entry.getValue();
System.out.println("Next:");
System.out.println("Person: " + a + " Hashcode: " + entry.hashCode() + " Value: " + b);
}
}
}
我做错了什么?
【问题讨论】:
-
哦,对不起。我没有得到输出“1015612567”。
-
什么都没有。有什么问题?
标签: java dictionary hashmap hashcode