由于是自定义类型,所以HashMap中的equals()函数和hashCode()函数都需要自定义覆盖。
不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals函数,应该就相当于c++重载==运算符来保证能判断是否相等。只不过java没有自定义重载运算符这个功能的,需要进行函数覆盖。
equals的函数原型是 boolean equals(Object o);注意括号内。hashCode的函数原型就是int hashCode();
先看一段代码:
import java.util.*;class Pair{
V first;
K second;public Pair() {first = null;second = null;}publicPair(V f,K s){
first=f;
second=s;
}//public boolean equals(Object o) {//if(!(o instanceof Pair))//{//return false;//}//Pair pn = (Pair)o;//return pn.first.equals(first) && pn.second.equals(second);//}//public int hashCode() {//return first.hashCode() + second.hashCode();//}
}public classMain {public static voidmain(String[] args) {
Pair p = new Pair<>("a","b");
Pair q = new Pair<>("a","b");
System.out.println(p.equals(q));
System.out.println(p.hashCode()+ " " +q.hashCode());
Map,Integer> map = newHashMap();
map.put(p,1);
System.out.println(map.containsKey(q));
map.put(q,2);for(Pairkey : map.keySet()) {
System.out.println(map.get(key));
}
}
}
这段代码把重载的两个函数给注释掉了,然后判断了值相同的两个Pair对象p和q是否相等,并输出了他们的hashCode,同时把p放进建立的HashMap中,用q来判断p是否存在,再把q放入,并遍历map的values。答案是:
false
2018699554 1311053135
false
1
2
然后:
import java.util.*;class Pair{
V first;
K second;public Pair() {first = null;second = null;}publicPair(V f,K s){
first=f;
second=s;
}public booleanequals(Object o) {if(!(o instanceofPair))
{return false;
}
Pair pn = (Pair)o;return pn.first.equals(first) &&pn.second.equals(second);
}public inthashCode() {return first.hashCode() +second.hashCode();
}
}public classMain {public static voidmain(String[] args) {
Pair p = new Pair<>("a","b");
Pair q = new Pair<>("a","b");
System.out.println(p.equals(q));
System.out.println(p.hashCode()+ " " +q.hashCode());
Map,Integer> map = newHashMap();
map.put(p,1);
System.out.println(map.containsKey(q));
map.put(q,2);for(Pairkey : map.keySet()) {
System.out.println(map.get(key));
}
}
}
然后把注释去掉之后的结果是:
true
195 195
true
2
虽然说这里的hashCode重载的没有什么道理,但是至少值相同的两个对象他们的hashCode是相同的,这样在HashMap中第一关判断hashCode是否相同就过了,接着再遍历判断是否有值相同的元素,可以判断是否含有某个键值,并更新键值的value等等。
而且要注意equals的覆盖,参数列表不可以因为是对于Pair的就改成Pair p,这样只是重载了并不会覆盖。