【问题标题】:HashSet or HashMap without defining a hashCode() method in a new classHashSet 或 HashMap 未在新类中定义 hashCode() 方法
【发布时间】:2016-09-24 20:18:53
【问题描述】:
  • 如果您设计一个新类并尝试将该类的对象插入 HashSet 或 HashMap 而不定义 hashCode() 方法,会发生什么情况?

请保持解释简单。我正在为考试而学习,但我仍然对 Java 中的哈希感到生疏。谢谢。

【问题讨论】:

  • 你不能只用几行代码自己测试一下吗?
  • this。如果您不覆盖 hashCode(),那么对于 HashSet 来说是一个问题,因为集合应该没有重复项,并且如果没有覆盖,每个对象都将被假定为唯一的(即使它们不是)。

标签: java hashmap hashcode hashset


【解决方案1】:

HashMap 将数据存储到多个条目的单链表(也称为桶或箱)中。所有的列表都注册在一个Entry数组中(Entry[]数组)

下图显示了一个 HashMap 实例的内部存储,其中包含一个可以为空的条目数组。每个Entry可以链接到另一个Entry,形成一个链表。

当用户调用 put(K key, V value) 或 get(Object key) 时,该函数计算 Entry 应该在的桶的索引。

桶的这个索引(链表)是使用键的哈希码生成的。 因此,如果您覆盖了 hashCode 方法,它将使用覆盖的方法来计算存储桶的索引 否则将使用默认哈希码,即对象的内存地址。因此,在这种情况下,即使是您的对象,您的地图中也会有一个新条目。因此,即使您尝试存储逻辑上相等的对象。它们将被哈希映射重新定义为不同的。

在合理的情况下,Object 类定义的 hashCode 方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但 JavaTM 编程语言不需要这种实现技术。)

例如:

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

【讨论】:

    【解决方案2】:

    什么都不会发生 :-)

    每个对象都有自己的 hashCode() 方法,继承自 Object 类。因此,您的每个新对象都将是独一无二的。它们自己会被 HashSet 或 HashMap 识别为唯一的。

    这里是官方的cmets:

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

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2023-02-14
      • 2016-05-28
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多