hashCode方法注释
Object 的 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}.
注释如是说:提供此方法支持的原因是,为了支持一些 hash tables ;
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.
在不修改 equals
方法的前提下,在一次程序执行期间,多次调用同一个对象的 hashCode
方法,得到的结果始终应该是一个相同的整数(Integer
类型);当然,如果在不同的程序中,此结论不成立;
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.
如果两个对象的 equals
方法返回值一样,则调用它们各自的 hashCode
方法,返回值也必须保证一样;
It is not 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.
对于 equals
方法返回值不同的对象,对其 hashCode
的返回值没有必须不同的要求,但是我们应该知道,对于 equals
方法返回值不同的对象,其 hashCode
方法最好也设计成返回不同的结果,这样有助于提高 hash tables 的性能 ;
Object
对象的 hashCode
方法,不同的对象在调用的时候,确实返回了不同的结果,得益于本地实现,返回了对象的内存地址 ;
equals 方法注释
It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}.
equals
方法具有 自反性,对于非 null
引用 x
, x.equals(x) 应该返回 true
;
It is symmetric: 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}. x
equals
方法具有 对称性,对于非 null
引用 x,y
, x.equals(y) 的返回值应该和 y.equals(x) 的返回值保持一致;
It is transitive: 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}.
equals
方法具有 传递性,对于非 null
引用 x,y,z
,如果 x.equals(y) 返回 true
, y.equals(z) 返回值为 true
,则 x.equals(z) 也必须返回 true
;
It is consistent: 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.
equals
方法具有 一致性,对于非 null
引用 x,y
,在没有修改 equals
方法的前提下,多次调用 x.equals(y) 的返回值,应该和多次调用 y.equals(z) 的返回值保持一致;
For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}
对于任何的非 null
引用 x
,x.equals(null) 返回 false
;
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.
记住了!我们在覆写 equals
方法的时候,最好,也同时覆写 hashCode
,以便保证 equals 方法返回值相同的对象,hashCode方法返回值也相同 ;
equals 方法
public boolean equals(Object obj) {
return (this == obj);
}
Object
类的 equals
方法,内部只是简单的判断对象的地址 ;
- List item