package com.sample;
import java.util.HashMap;
class Student{
int id;
@Override
public int hashCode() {
return -1;
}
@Override
public boolean equals(Object obj) {
return false; // returning false
}
}
public class MainClass {
public static void main(String[] args) {
Student s1=new Student();
s1.id=123;
Student s2=new Student();
s2.id=456;
HashMap<Student,String> s=new HashMap<Student,String>();
s.put(s1, "One");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
s.put(s2, "Two");
System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
s.put(s1, "Three");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
System.out.println("after insert");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
}
}
OUTPUT
< s1 value > One < s1 hashcode > 79430
< s2 value > Two < s2 hashcode > 84524
< s1 value > Three < s1 hashcode > 80786814
after insert
< s1 value > Three < s1 hashcode > 80786814 //printing three for s1
< s2 value > Two < s2 hashcode > 84524 //printing two for s2
//现在,如果我们将equals方法的返回类型更改为true,则输出更改,并且两者都返回三个作为输出。我无法理解如果我们更改equals方法的返回类型,为什么输出会更改。请使用bucket(HashMap)和equals方法的上下文进行说明。
class Student{
int id;
@Override
public int hashCode() {
return -1;
}
@Override
public boolean equals(Object obj) {
return true; //returning true
}
}
public class MainClass {
public static void main(String[] args) {
Student s1=new Student();
s1.id=123;
Student s2=new Student();
s2.id=456;
HashMap<Student,String> s=new HashMap<Student,String>();
s.put(s1, "One");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
s.put(s2, "Two");
System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
s.put(s1, "Three");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
System.out.println("after insert");
System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
}
}
OUTPUT-
< s1 value > One < s1 hashcode > 79430
< s2 value > Two < s2 hashcode > 84524
< s1 value > Three < s1 hashcode > 80786814
after insert
< s1 value > Three < s1 hashcode > 80786814 //printing three for s1
< s2 value > Three < s2 hashcode > 80786814 //printing three for s2
最佳答案
在第一个代码段中,您的equals
方法始终返回false
,这意味着HashMap
认为所有Student
实例都是唯一的。因此,s.get(s1)
和s.get(s2)
返回不同的值。
在第二个片段中,您的equals
方法始终返回true
,而您的hashCode
始终返回-1,这意味着HashMap
会将所有Student
实例视为相同。因此,s.get(s1)
和s.get(s2)
都返回相同的值(对put
的每次调用都将覆盖先前的值)。该值为“三”,因为这是您在地图中输入的最后一个值(通过调用s.put(s1, "Three");
)。
P.S.,打印s.get(s1).hashCode()
似乎毫无意义,因为键(hashCode
)的s1.hashCode()
决定了条目将存储在HashMap
中的存储区,而不是值的hashCode
。
顺便说一句,最初我很惊讶您的第一个代码片段在所有对null
的调用中都没有返回s.get()
,因为equals
总是返回false
,所以HashMap
不能找到是给定键的key
。但是,检查equal
的源代码后,我发现在调用HashMap
之前先将键与==
进行比较,这就是equals
可以找到您的键的原因。