equal与hashcode问答

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 return true .
  • It is symmetric : for any non-null reference values x and y , x.equals(y) should return true if and only if y.equals(x) returns true .
  • It is transitive : for any non-null reference values x , y , and z , if x.equals(y) returns true and y.equals(z) returns true , then x.equals(z) should return true .
  • It is consistent : for any non-null reference values x and y , multiple invocations of x.equals(y) consistently return true or consistently return false , provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x , x.equals(null) should return false .

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y , this method returns true if and only if x and y refer to the same object (x == y has the value true ).

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.

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值