Java中equals与hashcode
2017-11-19 21:18
169 查看
在JDK中,有相关约定,当a.equals(b)为true,那么a.hashcode()就必须与b.hashcode()相等,但是如果a.equals(b)为false,a与b的hashcode并没有强制要求不相等,但是正常做法应该a.equals(b)为false,那么a.hashcode也应该!=b.hashcode,因为这样可以提高hash表的性能。
以下是JDK中Object类的hashcode()注释:
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(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 hash tables.
当然,最重要的一点是,当我们要把对象作为HashMap的key时,或者放到HashSet容器中时,我们必须重写equals()和hashcode()方法,因为我们在put,get时,hashMap不只是比较了hashcode还有equals,只有两者都为true时才会认为是同一对象。
BTW:
在JDK1.7中引入了一个工具类java.util.Objects,日常使用的isNull,equals,compare我们可以使用这个工具类。
以下是JDK中Object类的hashcode()注释:
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(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 hash tables.
当然,最重要的一点是,当我们要把对象作为HashMap的key时,或者放到HashSet容器中时,我们必须重写equals()和hashcode()方法,因为我们在put,get时,hashMap不只是比较了hashcode还有equals,只有两者都为true时才会认为是同一对象。
BTW:
在JDK1.7中引入了一个工具类java.util.Objects,日常使用的isNull,equals,compare我们可以使用这个工具类。
相关文章推荐
- java中的equals( )和hashcode( )
- Java:重写equals()和hashCode()
- Java中Object类的equals()和hashCode()方法深入解析
- Java中的equals()和hashCode()契约
- Java重写equals()和hashCode()
- JAVA重写equals方法以及hashCode方法
- java 中hashcode 与 equals的关系
- Java中的equals和hashCode方法详解
- Java中equals、==、hashcode的区别
- Java:重写equals()和hashCode()
- Java中Object类的equals()和hashCode()方法深入解析
- Java 中 hashCode 和 equals 方法 小结
- java中hashcode()和equals()的详解
- java中hashcode和equals方法的关系
- Java中的equals方法与hashCode方法解析
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- java hashCode()和equals()
- Java中equals和==的区别,equals和hashcode的区别
- java高新技术【2】 java中equals,hashcode 和 == 【经典】
- Java中的equals()和hashCode()方法