equal与hashcode问答
1.hashcode()到底是干什么用的?好像是和数据结构的hash表有关?java对象怎么会和hash表有关的?java中的对象都是存在一个hash表中吗?
当你的对象存储在HashSet,HashMap,HsahTable等以hash表为工作机制的容器的时候 你才需要关心这个函数,不是java中对象都是存在一个hash表里面
2.为什么a.equal(b)==true则必定a.hashcode()==b.hashcode()。equal代表内容,hashcode代表内存地址,难道我理解错了吗?
谁说equals是比较内容的,默认的equals 就是与 == 一样的效果
3.很多类都重写了hashcode,比如Integer.hashcode返回的是Integer的intValue。他们为什么要重写呢?
就是因为Integer重写了equals() 方法 ,所以要重写hsahCode(),以保证a.equals(b) 为true时候,有a==b;重写只要为了满足可能把Integer对象放到hash容器里面
4.有人说a.equal(b)==true则必定a.hashcode()==b.hashcode();反之 a.hashcode()==b.hashcode()则不一定a.equal(b)==true; 那奇怪了,hashcode岂不是毫无用处了?大家都用hashcode做什么呢?
用hsahCode()主要是支持hash容器的正确运行,hash容器根据hashCode决定对象的存储位置
hash容器速度比较快
========================================================================
【JDK中的说明】
1,equals
public boolean equals (Object obj)
-
Indicates whether some other object is "equal to" this one.
The
equals
method implements an equivalence relation on non-null object references:- 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 of x.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
.
The equals method for class
Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
- It is reflexive : for any non-null reference value
-
-
Parameters:
-
obj
- the reference object with which to compare.
Returns:
-
true
if this object is the same as the obj argument;false
otherwise.
See Also:
-
hashCode()
,Hashtable
-
2,hashCode
public int hashCode ()
-
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by
java.util.Hashtable
.The general contract of
hashCode
is:- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in 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.
- If two objects are equal according to the equals(Object) method, then calling the
hashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal according to the
equals(java.lang.Object)
method, then calling the 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 hashtables.
As much as is reasonably practical, the hashCode method defined by class 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 JavaTM programming language.)
-
-
Returns:
- a hash code value for this object. See Also:
-
equals(java.lang.Object)
,Hashtable