没有在新的 class 中定义 hashCode() 方法的 HashSet 或 HashMap
HashSet or HashMap without defining a hashCode() method in a new class
- 如果您设计一个新的 class 并尝试在没有定义 hashCode() 方法的情况下将那个 class 的对象插入到 HashSet 或 HashMap 中,会发生什么情况?
请保持解释简单。我正在为考试而学习,但我仍然对 Java 中的哈希感到生疏。谢谢。
什么都不会发生:-)
每个对象都有自己的 hashCode() 方法,它继承自 Object class。因此,您的每个新对象都将是独一无二的。就其本身而言,它们将被 HashSet 或 HashMap 识别为唯一。
官方评论如下:
/**
* 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();
HashMap 将数据存储到多个单独 linked 的条目列表(也称为桶或容器)中。所有列表都注册在一个Entry数组中(Entry[]数组)
下图显示了一个 HashMap 实例的内部存储,其中包含一个可空条目数组。每个条目可以 link 到另一个条目以形成一个 linked 列表。
当用户调用 put(K key, V value) 或 get(Object key) 时,该函数会计算 Entry 所在的存储桶的索引。
存储桶的索引(linked 列表)是使用密钥的哈希码生成的。
因此,如果您重写了 hashCode 方法,它将使用重写的方法来计算存储桶的索引
否则将使用默认哈希码,它是您的对象的内存地址。因此,在这种情况下,即使您的对象是,您的地图中也会有一个新条目。所以即使你试图存储逻辑上相等的对象。它们将被哈希映射重新定义为不同。
在相当实用的情况下,class Object 定义的 hashCode 方法为不同的对象执行 return 不同的整数。 (这通常是通过将对象的内部地址转换为整数来实现的,但JavaTM 编程语言不需要这种实现技术。)
例如:
MyObject a = new MyObject("a", 123,"something");
MyObject b = new MyObject("a", 123,"something");
a and b will have different hashcodes.
- 如果您设计一个新的 class 并尝试在没有定义 hashCode() 方法的情况下将那个 class 的对象插入到 HashSet 或 HashMap 中,会发生什么情况?
请保持解释简单。我正在为考试而学习,但我仍然对 Java 中的哈希感到生疏。谢谢。
什么都不会发生:-)
每个对象都有自己的 hashCode() 方法,它继承自 Object class。因此,您的每个新对象都将是独一无二的。就其本身而言,它们将被 HashSet 或 HashMap 识别为唯一。
官方评论如下:
/**
* 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();
HashMap 将数据存储到多个单独 linked 的条目列表(也称为桶或容器)中。所有列表都注册在一个Entry数组中(Entry[]数组)
下图显示了一个 HashMap 实例的内部存储,其中包含一个可空条目数组。每个条目可以 link 到另一个条目以形成一个 linked 列表。
当用户调用 put(K key, V value) 或 get(Object key) 时,该函数会计算 Entry 所在的存储桶的索引。
存储桶的索引(linked 列表)是使用密钥的哈希码生成的。 因此,如果您重写了 hashCode 方法,它将使用重写的方法来计算存储桶的索引 否则将使用默认哈希码,它是您的对象的内存地址。因此,在这种情况下,即使您的对象是,您的地图中也会有一个新条目。所以即使你试图存储逻辑上相等的对象。它们将被哈希映射重新定义为不同。
在相当实用的情况下,class Object 定义的 hashCode 方法为不同的对象执行 return 不同的整数。 (这通常是通过将对象的内部地址转换为整数来实现的,但JavaTM 编程语言不需要这种实现技术。)
例如:
MyObject a = new MyObject("a", 123,"something");
MyObject b = new MyObject("a", 123,"something");
a and b will have different hashcodes.