Object类当中的equals和hashCode方法
**前言:**最近学习过程中,碰到了涉及需要对
equals
方法,和hashCode
方法的重写,发现对之前的java基础知识有点陌生了,所以有必要去复习一下,然后顺便记录一下。
我们知道equals
方法hashCode
都是Object
这个超类当中的方法。进入jdk的源码看一下。
1.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);
}
下面我作为你的翻译官,加上自己的理解翻译一下上面这段注释的主要内容:
指示其他某个对象是否与此对象“相等”。
equals 方法在非空对象引用上实现相等关系:
- 自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
- 对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
- 传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
- 一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
对于任何非空引用值 x,x.equals(null) 都应返回 false。 - 这个方法返回true当且仅当x和y指向了同样的对象(x==y),这句话也就是说明了在默认情况下,Object类中的equals方法默认比较的是对象的地址,因为只有是相同的地址才会相等(x == y),如果没有重写equals方法,那么默认就是比较的是地址。
- 注意:通常需要覆盖{hashCode}方法,以便维护 { hashCode}方法的通用契约,其中声明
相等的对象必须有相等的哈希码。(意思就是如果我们重写了equals方法的时候,必须重写hashcode)
2. 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();
同样解释一下上面这段话的意思:
返回对象的哈希码值。这个方法是所提供的哈希表的优点:
- (1)在同一次的java程序应用过程中,对应同样的对象多次调用hashCode方法,hashCode方法必须一致性的返回同样的一个地址值,前提是这个对象不能改变
- (2)如果两个对象根据{@code equals(Object)}相等方法,然后分别调用{hashCode}方法两个对象必须产生相同的整数结果。但是如果两个对象的equals方法不相等,并不一定要求{hashCode}返回不同的结果。然而,程序员应该注意产生不同的整数结果对于不相等的对象可以提高哈希表的性能。
看完这两段解释我稍微总结一下:
(1)对于equals方法,要满足这四个特性,自反性,对称性,一致性,传递性。根据equals方法相等,其hashcode的返回值也相等这一契约规定,当我们重新equals方法的时候,也必须重写hashcode方法.
(2)对于hashcode,当equals方法返回fals时,那么不强制规定,hashcode方法的返回值也必须不同。但是尽量产生不同的返回值,能提高我们hash表的性能。(如我们hashmap存储的时候,设计hash算法的时候,尽可能来使得每一个存储进来的不同key的hash值不同,这样就能减少碰撞,从而提高hash表的性能)。
希望能坚持写下去,记录点滴滴