重写equals()方法就必须重写hashCode()方法的原因
2017-09-27 16:13
344 查看
一.Object类中关于hashCode()方法的注释:
简单翻译一下,大概意思就是
1.在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
2.如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
3.如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
二、重写的意义
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,
当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,
导致混淆,因此,就也需要重写hashcode()
三、重写hashCode()的原则
1.同一个对象多次调用hashCode()方法应该返回相同的值;
2.当两个对象通过equals()方法比较返回true时,这两个对象的hashCode()应该返回相等的(int)值;
3.对象中用作equals()方法比较标准的Filed(成员变量(类属性)),都应该用来计算hashCode值。
查看String源码,看hashCode()d的实现方法:
/** * Returns a hash code value for the object. This method is * supported for the benefit of hashtables such as those provided by * <code>java.util.Hashtable</code>. * <p> * The general contract of <code>hashCode</code> is: * <ul> * <li>Whenever it is invoked on the same object more than once during * an execution of a Java application, the <tt>hashCode</tt> method * must consistently return the same integer, provided no information * used in <tt>equals</tt> 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. * <li>If two objects are equal according to the <tt>equals(Object)</tt> * method, then calling the <code>hashCode</code> method on each of * the two objects must produce the same integer result. * <li>It is <em>not</em> required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the <tt>hashCode</tt> 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. * </ul> * <p> * As much as is reasonably practical, the hashCode method defined by * class <tt>Object</tt> 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 * Java<font size="-2"><sup>TM</sup></font> programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.util.Hashtable */ public native int hashCode();
简单翻译一下,大概意思就是
1.在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
2.如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
3.如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
二、重写的意义
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,
当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,
导致混淆,因此,就也需要重写hashcode()
三、重写hashCode()的原则
1.同一个对象多次调用hashCode()方法应该返回相同的值;
2.当两个对象通过equals()方法比较返回true时,这两个对象的hashCode()应该返回相等的(int)值;
3.对象中用作equals()方法比较标准的Filed(成员变量(类属性)),都应该用来计算hashCode值。
查看String源码,看hashCode()d的实现方法:
/** * Returns a hash code for this string. The hash code for a * <code>String</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the string, <code>n</code> is the length of * the string, and <code>^</code> indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }
相关文章推荐
- 详解重写equals()方法就必须重写hashCode()方法的原因
- 详解重写equals()方法就必须重写hashCode()方法的原因
- 详解重写equals()方法就必须重写hashCode()方法的原因
- 为什么在重写了equals()方法之后也必须重写hashCode()方法
- 为什么在重写了equals()方法之后也必须重写hashCode()方法
- 判断两个对象是否相等,为什么必须同时重写equals()和hashcode()方法
- 为什么使用HashSet储存对象时必须重写equals和hashcode方法
- why在重写equals时还必须重写hashcode方法
- java中为什么重写equals时必须重写hashCode方法?
- HashSet中的元素必须重写equals方法和hashCode方法
- 为什么在重写 equals方法的同时必须重写 hashcode方法
- 判断两个对象是否相等,为什么必须同时重写equals()和hashcode()方法
- 重写equals方法时必须重写hashCode方法的理由
- Set是如何实现元素不重复的&重写equals()方法之后也必须重写hashCode()方法
- Java 重写equals方法为什么还必须重写hashCode方法
- 为什么在重写了equals()方法之后也必须重写HashCode()方法
- 重写equals时还必须重写hashcode方法,详解
- 重写equals方法一般需要重写hashCode方法的原因
- 重写equals时还必须重写hashCode方法
- 为什么重写equals时必须重写hashCode方法