equals() 和 hashCode() 的区别 [英] difference between equals() and hashCode()

查看:32
本文介绍了equals() 和 hashCode() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个关于 equals() 、 "==" 和 hashCode() 的简要定义.如果我运行以下代码意味着输出将是true false 2420395 2420395".但我知道 equals() 方法比较字符串,=="比较引用.但是在输出中,hashCcode() 方法将两个字符串的参考号打印为相同,那么为什么=="返回false".

I want a brief definition about the equals() , "==" and hashCode(). If i run following code means the output will be "true false 2420395 2420395". But i had understand that equals() method compares the string and "==" compares the reference. But in output the hashCcode() method prints the reference number for both strings as same then why the "==" returns "false".

            String str = "Name";
    String str1 = new String("Name");

    if(str.equals(str1))
        System.out.println("true");
    else
        System.out.println("false");
    if(str==str1)
        System.out.println("true");
    else
        System.out.println("false");

    System.out.println(str.hashCode());
    System.out.println(str1.hashCode());
}

推荐答案

equals()hashCode() 方法被证明是非常重要的,当对象实现这些将两种方法添加到集合中.如果实施不当,可能会搞砸你的生活.

The equals() and hashCode() methods prove to be very important, when objects implementing these two methods are added to collections. If implemented incorrectly it might screwed up your life.

equals() :此方法检查作为参数传递给它的其他对象是否等于调用此方法的对象.如果您不了解合同,很容易错误地实现 equals() 方法.在覆盖此方法之前,需要牢记以下属性" -

equals() : This method checks if some other object passed to it as an argument is equal the object in which this method is invoked. It is easy to implement the equals() method incorrectly, if you do not understand the contract. Before overriding this method, following "properties" need to keep in mind -

  • 自反:o1.equals(o1) - 这意味着一个对象(例如 o1)应该等于它自己
  • 对称:o1.equals(o2) 当且仅 o2.equals(o1)
  • 传递:o1.equals(o2) &&o2.equals(o3) 意味着 o1.equals(o3) 也是如此
  • 一致:只要 o1 和 o2 未被修改,o1.equals(o2) 就返回相同的结果
  • null 比较:!o1.equals(null) - 这意味着任何可实例化的对象都不等于 null.因此,如果您将 null 作为参数传递给对象 o1,那么它应该返回 false.
  • 哈希码值:o1.equals(o2) 意味着 o1.hashCode() == o2.hashCode() .这是非常重要的.如果你定义了一个 equals() 方法,那么你也必须定义一个 hashCode() 方法.这也意味着,如果你有两个相等的对象,那么它们必须具有相同的 hashCode,但反之则不正确

来自java源代码

*
* @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);

}

hashCode():此方法以整数形式返回 hashCode() 值,并支持基于散列的 java.util.Collection 类,如 Hashtable、HashMap、HashSet 等.如果类重写了equals()方法,它也必须实现hashCode()方法.在重写这个方法之前,你需要记住

hashCode(): This method returns a hashCode() value as an Integer and is supported for the benefit of hashing based java.util.Collection classes like Hashtable, HashMap, HashSet etc. If a class overrides the equals() method, it must implement the hashCode() method as well.Before overriding this method, you need to keep in mind

  • 在 Java 程序的执行过程中,当对同一个对象多次调用 hashCode() 方法时,该方法必须始终返回相同的结果.从程序的一次执行到同一程序的下一次执行,整数结果不必保持一致.
  • 如果根据 equals() 方法,两个对象相等,则在两个对象中调用 hashCode() 方法必须返回相同的整数结果.所以,如果一个字段没有在 equals() 中使用,那么它一定不能在 hashCode() 方法中使用.

  • Whenever hashCode() method is invoked on the same object more than once during an execution of a Java program, this method must consistently return the same result. The integer result need not remain consistent from one execution of the program to the next execution of the same program.
  • If two objects are equal as per the equals() method, then calling the hashCode() method in each of the two objects must return the same integer result. So, If a field is not used in equals(), then it must not be used in hashCode() method.

如果两个对象根据equals()方法不相等,则两个对象中的每一个都可以返回两个不同的整数结果或相同的整数结果(即如果2个对象具有相同的hashCode()结果并不意味着它们相等,但如果两个对象相等,则它们必须返回相同的 hashCode() 结果).

If two objects are unequal as per the equals() method, each of the two objects can return either two different integer results or same integer results (i.e. if 2 objects have the same hashCode() result does not mean that they are equal, but if two objects are equal then they must return the same hashCode() result).

根据java源代码java.lang.Object 定义的 hashCode 方法在合理可行的情况下确实为不同的对象返回不同的整数.(这通常通过将对象的内部地址转换为整数来实现)

As per java source code As much as is reasonably practical, the hashCode method defined by java.lang.Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer)

这篇关于equals() 和 hashCode() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆