hashCode()方法
首先來看看hashCode()在源代碼中的注釋(java8):
/**
* 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();
作用是:為對象返回一個(gè)hash值润努,為支持哈希表提供了很好的支持舒帮。
hashCode的約定是:
- 在一個(gè)java程序執(zhí)行期間,如果對象中用于等價(jià)比較的信息沒有改變咒锻,同一個(gè)對象無論何時(shí)超過一次執(zhí)行,該方法都返回相同的整數(shù)。這個(gè)哈希值在程序的一個(gè)執(zhí)行到該程序的另一個(gè)執(zhí)行不需要保持一致漓摩。
- 如果兩個(gè)對象通過equals()判斷為相等的,那么調(diào)用兩個(gè)對象的hashCode()肯定也返回相同的值陈瘦。
- 通過equals()方法判斷不相等的兩個(gè)對象的hash值并不意味是不同的幌甘。但是程序員要意識到不同對象產(chǎn)生不同的哈希值有助于提高哈希表的性能潮售。
最佳實(shí)踐是類中定義的hashCode方法,不同的對象返回不同的值锅风。(通常的實(shí)現(xiàn)是將對象的內(nèi)部地址轉(zhuǎn)換為一個(gè)值酥诽,但是這個(gè)技術(shù)實(shí)現(xiàn)不是java變成語言所必須的)
equals()方法
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
- 自反性:對于任意非空引用x,x.equals(x)一定返回true
- 對稱性:對于任意非空引用x,y皱埠,如果x.equals(y)返回true肮帐,那么y.equals(x)也一定返回true
- 傳遞性:對于任意非空引用x,y,z,如果x.equals(y)返回true和y.equals(z)返回true边器,那么x.equals(z)也一定返回true
- 一致性:對于任意非空引用x,y,如果對象中用于等價(jià)比較的信息沒有改變训枢,那么無論調(diào)用多少次x.equals(y)返回的結(jié)果都是一致的,要么是true忘巧,要么是false
- 對任何不是null的x,x.equals(null)一定返回false
Object類的equals()方法實(shí)現(xiàn)的是最挑剔的可能等價(jià)的關(guān)系恒界。也就是說,對于任何非空引用x,y,當(dāng)且僅當(dāng)x,y引用于同一個(gè)對象才會返回true
需要注意的是砚嘴,當(dāng)該方法被重寫時(shí)通常需要重寫hashCode()方法十酣,符合hashCode()方法的約定,即平等對象必須有相同的哈希碼
為什么通常重寫equals還要重寫hashCode()的方法际长,因?yàn)樵趈ava集合類有關(guān)散列表的實(shí)現(xiàn)中耸采,通常不僅僅判斷equals方法,還會判斷hashCode()方法
public class User {
private String name;
private int age;
public User(){}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (age != user.age) return false;
return name != null ? name.equals(user.name) : user.name == null;
}
public static void main(String[] args) {
Map<User,String> map = new HashMap<>();
map.put(new User("a",1),"1");
System.out.println(map.get(new User("a",1)));
}
只重寫equals方法后工育,輸出為null,主要原因在于HashMap的判斷方法同時(shí)判斷hash值和equals(),
而一般默認(rèn)Object的hashCode()方法是映射于對象的存儲地址虾宇。
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//判斷
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
//判斷
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
也重寫hashCode方法:
public class User {
private String name;
private int age;
public User(){}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (age != user.age) return false;
return name != null ? name.equals(user.name) : user.name == null;
}
@Override
public int hashCode() {
return name.hashCode()*37+age;
}
public static void main(String[] args) {
Map<User,String> map = new HashMap<>();
map.put(new User("a",1),"1");
System.out.println(map.get(new User("a",1)));
}
}
輸出的為1,是正確的