Java HashSet或HashMap,而不在新类中定义hashCode()方法 如果设计一个新类并尝试将该类的对象插入HashSet或HashMap而不定义hashCode()方法,会发生什么情况
请尽量使解释容易些。我正在准备考试,但我对Java中的哈希仍然生疏。谢谢。什么都不会发生:-) 每个对象都有自己的hashCode()方法,该方法继承自对象类。所以,你的每一个新对象都是独一无二的。通过HashSet或HashMap,它们将被标识为唯一的 以下是官方评论:Java HashSet或HashMap,而不在新类中定义hashCode()方法 如果设计一个新类并尝试将该类的对象插入HashSet或HashMap而不定义hashCode()方法,会发生什么情况,java,hashmap,hashcode,hashset,Java,Hashmap,Hashcode,Hashset,请尽量使解释容易些。我正在准备考试,但我对Java中的哈希仍然生疏。谢谢。什么都不会发生:-) 每个对象都有自己的hashCode()方法,该方法继承自对象类。所以,你的每一个新对象都是独一无二的。通过HashSet或HashMap,它们将被标识为唯一的 以下是官方评论: /** * Returns a hash code value for the object. This method is * supported for the benefit of hash tables such
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code 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.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} 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 {@code 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.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code 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
* Java™ programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();
/**
*返回对象的哈希代码值。这种方法是可行的
*支持哈希表,例如
*{@link java.util.HashMap}。
*
*{@code hashCode}的总合同是:
*
*- 在执行过程中对同一对象多次调用时
*Java应用程序{@code hashCode}方法的执行
*必须始终返回相同的整数,但不提供任何信息
*在{@code equals}比较中使用的对象已修改。
*此整数不需要从一次执行
*应用程序到同一应用程序的另一个执行。
*
- 如果根据{@code equals(Object)}两个对象相等
*方法,然后对每个
*这两个对象必须产生相同的整数结果。
*
- 如果两个对象不相等,则不需要
*根据{@link java.lang.Object#equals(java.lang.Object)}
*方法,然后对每个
*两个对象必须产生不同的整数结果。但是,
*程序员应该知道生成不同的整数结果
*对于不相等的对象,可以提高哈希表的性能。
*
*
*在合理可行的情况下,hashCode方法由
*类{@code Object}为distinct返回不同的整数
*对象。(这通常通过转换内部
*将对象的地址转换为整数,但此实现
*技术是不需要的
*Java&trade;编程语言。)
*
*@返回此对象的哈希代码值。
*@see java.lang.Object#equals(java.lang.Object)
*@see java.lang.System#identityHashCode
*/
公共本机int hashCode();
哈希映射将数据存储到多个单独链接的条目列表(也称为bucket或bin)中。所有列表都注册在条目数组中(条目[]数组)
下图显示了一个HashMap实例的内部存储,该实例包含一个可为空的条目数组。每个条目都可以链接到另一个条目以形成链接列表
当用户调用put(K键,V值)或get(Object键)时,该函数将计算该条目应包含在其中的bucket的索引。
bucket(链表)的索引是使用键的hashcode生成的。
所以,如果您已经重写了hashCode方法,它将使用重写的方法来计算bucket的索引
否则将使用默认哈希代码,它是对象的内存地址。因此,在这种情况下,即使您的对象是,您也将在地图中有一个新条目。因此,即使您尝试存储逻辑上相等的对象。它们将通过哈希映射被视为不同。
只要是合理可行的,类对象定义的hashCode方法确实会为不同的对象返回不同的整数。(这通常通过将对象的内部地址转换为整数来实现,但JavaTM编程语言不需要这种实现技术。)
例如:
MyObject a = new MyObject("a", 123,"something");
MyObject b = new MyObject("a", 123,"something");
a and b will have different hashcodes.
你不能自己用几行代码来测试这个吗。如果不重写
hashCode()
,则是一个问题,特别是对于HashSet
,因为集合应该没有重复项,如果没有重写,则每个对象都将被假定为唯一的(即使它们不是)。