http://community.csdn.net/Expert/topic/3871/3871956.xml?temp=.5207636
楼主到这里看看有没有你需要的吧

解决方案 »

  1.   

    如果这个类没有重写equals方法,那么他们的equals方法就是用Object类的
    而Object类的默认实现就是比较hashCode(),所以这个问题是显而易见的
    也就说hashCode()不相等,但是equals可能不相等而你这个问题,好象不太对啊
    如果hashCode()相等也就是说这两个句柄引用的是同一个对象,那么他们equals肯定是true呀
      

  2.   

    a_Object.equals(b_Object) 判断的是这两个字符串是否想同
      

  3.   

    to jFresH_MaN(受不了你了!!呆鸟!!啊!!啊!!):
    a_Object.hashCode()==b_Object.hashCode()返回true
    a_Object.equals(b_Object)不一定返回true.
    我在网上查了的。
      

  4.   

    b_Object.a_Object.equals(b_Object)比较的是这两个对象中的值是否相等.
    a_Object.hashCode()==b_Object.hashCode()比较的地址.一般情况下是有可能equals为真,而==为假.
    但是在自己重写了hashCode的话就会出现楼主说的情况.
    比如在hibernate的po中,一般都是用id值来比较的.所以有时量个对象的其他属性值不相同而id相同的话就会出现上述情况.
      

  5.   

    Integer i = new Integer(7);
    Long l = new Long(7);
    System.out.println("l.equals(i) : " + l.equals(i));
    System.out.println(l.hashCode() == i.hashCode());
    这段代码运行的结果就是false ,true
      

  6.   

    Equals method overridden in class Boolean returns true if and only if the object passed to it is a non-null Boolean and represents the same boolean value as this object.
    翻译这句话有100分
      

  7.   

    首先说明一点:
    在没有重载hashCode()和equals()方法的前提下
    如果
    b_Object.a_Object.equals(b_Object)

    a_Object.hashCode()==b_Object.hashCode()但是
    a_Object.hashCode()==b_Object.hashCode()
    却不能说明
    b_Object.a_Object.equals(b_Object)原因是因为Object的equals方法比较的是两个对象的引用
        public boolean equals(Object obj) {
    return (this == obj);
        }
    而hashCode是根据一定算法计算出来的一个int类型的数值,这个算法不管如何精确,因为返回的是一个整数出现相同的结果,但是是不同的对象的情况肯定有的。打个比方,假设int类型的最大值是65535,如果你有65536个实例,怎么都有两个是相同的hashCode,不同的对象的。
      

  8.   

    对这一点JDK文档中说明的非常好。
    The general contract of hashCode is: 1,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. 
    2,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. 
    3,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. 
    好好看看2和3
      

  9.   

    Equals  method  overridden  in  class  Boolean  returns  true  if  and  only  if  the  object  passed  to  it  is  a  non-null  Boolean  and  represents  the  same  boolean  value  as  this  object.  
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分 
    翻译这句话有100分
      

  10.   

    翻译:
    Equals  method  overridden  in  class  Boolean  returns  true  if  and  only  if  the  object  passed  to  it  is  a  non-null  Boolean  and  represents  the  same  boolean  value  as  this  object.  
    中文:
    对于在类Boolean中被overridden的方法equals(),仅当已经被传递的对象不为空,切有一样的值的时候返回true.不知道对不对哦:)
      

  11.   

    Equals  method  overridden  in  class  Boolean  returns  true  if  and  only  if  the  object  passed  to  it  is  a  non-null  Boolean  and  represents  the  same  boolean  value  as  this  object.  
    个人认为应该是这样 : boolean result = a.equals(b);  result==true 当且仅当 b!=null && a和b中的值相同(不是地址相同)。
      

  12.   

    晕楼主没有看我说的吗???
    也就说hashCode()相等,但是equals可能不相等哎
      

  13.   

    Equals  method  overridden  in  class  Boolean  returns  true  if  and  only  if  the  object  passed  to  it  is  a  non-null  Boolean  and  represents  the  same  boolean  value  as  this  object.  覆写的Boolean类里面的equals方法只有当当前对象和传进来的一个非null Boolean对象所包含的boolean值相等,才会返回true
      

  14.   

    楼主运行一下这个,看看结果可能会有所帮助吧
    String a = new String ("abc");
    String b = new String ("abd");

    System.out.println(a.hashCode()==b.hashCode());
    System.out.println(a.hashCode());
    a = "bcd";
    System.out.println(a.hashCode());
    System.out.println(b.hashCode());
    System.out.println(a==b);
    System.out.println(a.equals(b));此外,再看看KillAllError(无心) 的回帖,if(a.equals(b)) --》 a.hashCode()==b.hashCode()
    但是 if(!a.equals(b)) 不一定就能推论出 a.hashCode()!=b.hashCode()  因为a.hashCode()!=b.hashCode()  的原因是因为a和b在hashtable中的值不同,而不肯定是因为(!a.equals(b)) 引起的。如果有说的不对的地方,还请大家指点
      

  15.   

    也就是说equals方法和hashCode方法没有一种必然的因果关系。