转自:http://blog.****.net/neosmith/article/details/17068365
hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们就讨论一下如何正解理解和使用这2个方法.
如何重写equals()方法
- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and
only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
,
thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or
consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
这段话用了很多离散数学中的术数.简单说明一下:
- class Coder {
- private String name;
- private int age;
- // getters and setters
- }
我们想要的是,如果2个程序员对象的name和age都是相同的,那么我们就认为这两个程序员是一个人.这时候我们就要重写其equals()方法.因为默认的equals()实际是判断两个引用是否指向内在中的同一个对象,相当于 == . 重写时要遵循以下三步:
- if(other == this)
- return true;
2. 使用instanceof运算符判断 other 是否为Coder类型的对象.
- if(!(other instanceof Coder))
- return false;
3. 比较Coder类中你自定义的数据域,name和age,一个都不能少.
- Coder o = (Coder)other;
- return o.name.equals(name) && o.age == age;
看到这有人可能会问,第3步中有一个强制转换,如果有人将一个Integer类的对象传到了这个equals中,那么会不会扔ClassCastException呢?这个担心其实是多余的.因为我们在第二步中已经进行了instanceof
的判断,如果other是非Coder对象,甚至other是个null, 那么在这一步中都会直接返回false,
从而后面的代码得不到执行的机会.
如何重写hashCode()方法
hashCode
method whenever this method(equals) is overridden, so as to maintain the generalcontract for the
hashCode
method, which states that equal objects must have equal hash codes."Java>中给出了一个能最大程度上避免哈希冲突的写法,但我个人认为对于一般的应用来说没有必要搞的这么麻烦.如果你的应用中HashSet中需要存放上万上百万个对象时,那你应该严格遵循书中给定的方法.如果是写一个中小型的应用,那么下面的原则就已经足够使用了:
- @Override
- public int hashCode() {
- int result = 17;
- result = result * 31 + name.hashCode();
- result = result * 31 + age;
- return result;
- }
其中int result = 17你也可以改成20, 50等等都可以.看到这里我突然有些好奇,想看一下String类中的hashCode()方法是如何实现的.查文档知:
String
object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int
arithmetic, where s[i]
is the ith character of the string, n
is the length of the string, and ^
indicates exponentiation. (The hash value of the empty string is zero.)"
重写equals()而不重写hashCode()的风险

table大致相当于哈希表与链表的结合体.即在每个bucket上会挂一个链表,链表的每个结点都用来存放对象.Java通过hashCode()方法来确定某个对象应该位于哪个bucket中,然后在相应的链表中进行查找.在理想情况下,如果你的hashCode()方法写的足够健壮,那么每个bucket将会只有一个结点,这样就实现了查找操作的常量级的时间复杂度.即无论你的对象放在哪片内存中,我都可以通过hashCode()立刻定位到该区域,而不需要从头到尾进行遍历查找.这也是哈希表的最主要的应用.
o)方法时,首先会根据o.hashCode()的返回值定位到相应的bucket中,如果该bucket中没有结点,则将 o
放到这里,如果已经有结点了, 则把 o 挂到链表末端.同理,当调用contains(Object
o)时,Java会通过hashCode()的返回值定位到相应的bucket中,然后再在对应的链表中的结点依次调用equals()方法来判断结点中的对象是否是你想要的对象.
- Coder c1 = new Coder("bruce", 10);
- Coder c2 = new Coder("bruce", 10);
假定我们已经重写了Coder的equals()方法而没有重写hashCode()方法:
- @Override
- public boolean equals(Object other) {
- System.out.println("equals method invoked!");
- if(other == this)
- return true;
- if(!(other instanceof Coder))
- return false;
- Coder o = (Coder)other;
- return o.name.equals(name) && o.age == age;
- }
然后我们构造一个HashSet,将c1对象放入到set中:
- Set<Coder> set = new HashSet<Coder>();
- set.add(c1);
再执行:
- System.out.println(set.contains(c2));
B 的hashCode()要返回相同的值.
我让hashCode()每次都返回一个固定的数行吗
- @Override
- public int hashCode() {
- return 10;
- }
如果这样的话,HashMap, HashSet等集合类就失去了其 "哈希的意义".用<Effective
Java>中的话来说就是,哈希表退化成了链表.如果hashCode()每次都返回相同的数,那么所有的对象都会被放到同一个bucket中,每次执行查找操作都会遍历链表,这样就完全失去了哈希的作用.所以我们最好还是提供一个健壮的hashCode()为妙.