為了弄清為什么重寫equals()方法時炉擅,必須重寫hashCode()方法辉懒,我們首先需要明確Object實現(xiàn)hashCode()返回值是什么?
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code hashCode} method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java™ programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();
哈希碼的通用約定如下:
- 在java程序執(zhí)行過程中谍失,在一個對象沒有被改變的前提下眶俩,無論這個對象被調(diào)用多少次,hashCode()方法都會返回相同的整數(shù)值快鱼。對象的哈希碼沒有必要在不同的程序中保持相同的值颠印。
- 如果兩個對象使用equals()方法進行比較是等價的,那么這兩個對象的hashCode()方法必須返回相同的值抹竹。
- 如果兩個對象使用equals()方法進行比較是不等價的线罕,那么這兩個對象的hashCode()方法返回值不必相同。但是窃判,不等價的對象的hashCode()值不同的話可以提高哈希表的性能钞楼。
hashCode()方法是一個native方法,它返回的是由對象存儲地址轉(zhuǎn)化得到的值袄琳。
若重寫equals()方法而不重寫hashCode()方法询件,當(dāng)兩個對象使用equals()方法進行比較是等價的時,兩個對象的hashCode()方法返回值是不想等的(兩個對象的存儲地址是不相同的)唆樊,違背了哈希碼通用約定第二條宛琅。
下面的代碼中,新建了兩個等價的對象person1和person2逗旁,并將person1作為key存入HashMap中嘿辟。我們希望將這兩個對象當(dāng)成一個key,在HashMap中取person2的時候可以返回鍵person1的value片效。由于只重寫了equals()方法红伦,而未重寫hashCode()方法,實際返回值為null淀衣。
public class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this.id == ((Person)obj).id)
return true;
return false;
}
}
public class Test {
public static void main(String[] args) {
Person person1 = new Person(1, "Lisa");
Person person2 = new Person(1, "Lisa");
HashMap<Person, Integer> persons = new HashMap<>();
persons.put(person1, 10);
System.out.println(persons.get(person2));
}
}
null
String同時重寫了equals()方法和hashCode()方法
public class Test {
public static void main(String[] args) {
HashMap<String, Integer> strs = new HashMap<>();
String str1 = new String("ddd");
String str2 = new String("ddd");
strs.put(str1, 10);
System.out.println(strs.get(str2));
}
}
10