简体   繁体   English

HashSet 或 HashMap 没有在新类中定义 hashCode() 方法

[英]HashSet or HashMap without defining a hashCode() method in a new class

  • What happens if you design a new class and try to insert objects of that class into a HashSet or HashMap without defining a hashCode() method?如果您设计一个新类并尝试将该类的对象插入 HashSet 或 HashMap 而不定义 hashCode() 方法,会发生什么情况?

Please keep make the explanation easy.请保持简单的解释。 I'm studying for an exam and I'm still rusty with hashes in Java.我正在为考试而学习,但我仍然对 Java 中的哈希感到生疏。 Thank you.谢谢你。

A HashMap stores data into multiple singly linked lists of entries (also called buckets or bins). HashMap 将数据存储到多个单向链接的条目列表(也称为桶或箱)中。 All the lists are registered in an array of Entry (Entry[] array)所有列表都注册在一个Entry数组中(Entry[]数组)

The following picture shows the inner storage of a HashMap instance with an array of nullable entries.下图显示了一个带有可空条目数组的 HashMap 实例的内部存储。 Each Entry can link to another Entry to form a linked list.每个 Entry 可以链接到另一个 Entry 以形成一个链表。

When a user calls put(K key, V value) or get(Object key), the function computes the index of the bucket in which the Entry should be.当用户调用 put(K key, V value) 或 get(Object key) 时,该函数会计算 Entry 所在的桶的索引。 在此处输入图片说明

This index of the bucket (linked list) is generated using hashcode of the key.存储桶(链表)的这个索引是使用键的哈希码生成的。 So, if you have overridden the hashCode method, it will use overridden method to compute index of the bucket otherwise default hash code is used which is the memory address for your object.因此,如果您覆盖了 hashCode 方法,它将使用覆盖的方法来计算存储桶的索引,否则将使用默认哈希码,它是您对象的内存地址。 So in that case even your objects are you will have a new entry in your map.因此,在这种情况下,即使您的对象是,您的地图中也会有一个新条目。 So even if you try to store logically equal objects.因此,即使您尝试存储逻辑上相等的对象。 They wil be reataed as different by hash Map.它们将被哈希映射重新定义为不同。

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.就合理实用而言,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。 (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) (这通常是通过将对象的内部地址转换为整数来实现的,但是 JavaTM 编程语言不需要这种实现技术。)

For example:例如:

MyObject a = new MyObject("a", 123,"something");
MyObject b = new MyObject("a", 123,"something");
a and b will have different hashcodes.

Nothing will happen :-)什么都不会发生:-)

Every object has own hashCode() method that inherited from Object class.每个对象都有自己的 hashCode() 方法,该方法继承自Object类。 So, your every new object will be unique.因此,您的每个新对象都将是独一无二的。 By herself, they will be identified as unique by HashSet or HashMap.由她自己,它们将被 HashSet 或 HashMap 标识为唯一的。

Here are official comments:以下是官方评论:

/**
 * 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&trade; 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM