gpt4 book ai didi

java - 在 HashSet.contains() 如果 hashcode 返回常量值的情况下调用 hashCode() 和 equals() 的次数

转载 作者:行者123 更新时间:2023-12-02 15:26:36 26 4
gpt4 key购买 nike

我已经通读了 Java 文档页面,但我无法解释,为什么 hashCode()equals() 的调用次数会像这样变化?

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class NumberOfCalls {
int field;

public int getField() {
return field;
}

public NumberOfCalls(int field) {
this.field = field;
}

@Override
public int hashCode() {
System.out.println("In Hashcode method.");
return 10;
}

@Override
public boolean equals(Object obj) {
System.out.println("In Equals Method");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NumberOfCalls other = (NumberOfCalls) obj;
if (field != other.field)
return false;
return true;
}

public static void main(String[] args) {
NumberOfCalls object1 = new NumberOfCalls(5);
NumberOfCalls object2 = new NumberOfCalls(6);
NumberOfCalls object3 = new NumberOfCalls(5);
Set<NumberOfCalls> set = new HashSet<NumberOfCalls>();
set.add(object1);
set.add(object2);
Iterator<NumberOfCalls> it = set.iterator();
System.out.print("Size of set is : " + set.size()
+ "\nObject fields values present in set are : ");
while (it.hasNext()) {
System.out.print(it.next().getField() + " ");
}
System.out.println("\n---------------------------------------------------");
System.out.println("Now checking number of calls -- ");
System.out.println("Object1 is present in set ? - " + set.contains(object1)+"\n");
System.out.println("Object2 is present in set ? - " + set.contains(object2)+"\n");
System.out.println("Object3 is present in set ? - " + set.contains(object3)+"\n");
}
}

以上代码的输出是

In Hashcode method.
In Hashcode method.
In Equals Method
Size of set is : 2
Object fields values present in set are : 6   5

---------------------------------------------------
Now checking number of calls --
In Hashcode method.
In Equals Method
Object1 is present in set ? - true

In Hashcode method.
Object2 is present in set ? - true

In Hashcode method.
In Equals Method
In Equals Method
Object3 is present in set ? - true

问题:

  1. 为什么 hashCode()equals()object1 而不是 object2 的情况下调用一次> (在这种情况下只调用了 hashCode())?
  2. 为什么 equals()object3 的情况下被调用两次?

最佳答案

当您将元素添加到 Set 时,它会在内部存储在 Map 中,其中 key 是您传入的对象,value 设置为 nullMap 内部维护一个链表数组(桶)。这些数组也可以称为桶。使用 hashCode() 方法评估存储桶的索引。

在您的情况下,由于 hashCode() 返回一个常量值,因此放入 Set 的所有值都将放入同一个桶中。所以对于第一次调用 set.add(object1) 内部结构将类似于

bucket [object1 -> null]

由于每个桶都包含一个链表,存储在链表中的元素有一个指向下一个元素的指针。由于只有一个元素被添加到列表中,因此指针指向 null

对于下一次调用set.add(object2) 数据结构将如下所示

bucket [object2 -> object1 -> null]

现在,无论何时调用 set.contains(some_object),都会调用 hashCode() 来找出正确的桶。此调用还返回对第一个元素的引用。

第一个问题的答案:

因此,当您调用 set.contains(object2) 时,它实际上返回对 object2 的引用。现在,如果您查看 HashMap 类的代码,它首先使用 == 运算符比较此引用是否与传入的引用相同。因为在这种情况下它是相同的所以它不调用 equals() 方法。以下是 HashMap 类的代码片段:

if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;

这应该可以解释您的第一个问题。

第二个问题的答案:

当您调用 set.contains(object3) 时,会计算存储桶索引并返回对 object2 的引用。现在 object2 == object3 返回 false 所以 Map 调用 object3.equals(object2) 方法(首先调用 equals() 方法)来检查是否这两个对象在意义上是等价的。这也返回 false,因此它遍历列表并返回对列表中下一个元素的引用,即 object1

object1 == object3 再次返回 false,因此 Map 调用 object3.equals(object1)(第二次调用 equals() 方法)方法检查两个对象是否有意义等同。

这导致对 equals() 方法的 2 次调用。

关于java - 在 HashSet.contains() 如果 hashcode 返回常量值的情况下调用 hashCode() 和 equals() 的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com