HashSet containsAll 未按预期工作 - 尽管已实现 hashCode 和 equals 方法

发布于 2025-01-05 15:50:20 字数 2246 浏览 2 评论 0原文

有两个集合具有相同的单个元素。

Set<Activity> a = new HashSet<Activity>();
a.add(new Activity("X", 1, 2));
Set<Activity> b = new HashSet<Activity>();
b.add(new Activity("X", 1, 2));
return a.containsAll(b); //gives false??

containsAll 方法返回 false,而事实上这些集合是相同的?

我已阅读类似问题的答案,其中之一是 HashSet 包含自定义对象的问题, HashSet 包含方法,行为奇怪我了解 HashSet 是如何工作的。您计算存储桶的哈希值,然后使用重写的 equals() 方法搜索对象。

Activity 类是这样实现的:

public class Activity implements Comparable<Activity> {
  private final double maxDuration;
  private final double normalDuration;
  private final String label;   


  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((label == null) ? 0 : label.hashCode());
    long temp;
    temp = Double.doubleToLongBits(maxDuration);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(normalDuration);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
  }

    @Override
   public boolean equals(Object obj) {
      if (this == obj)
        return true;
      if (obj == null)
        return false;
      if (getClass() != obj.getClass())
        return false;
      Activity other = (Activity) obj;
      if (label == null) {
        if (other.label != null)
          return false;
   } else if (!label.equals(other.label))
        return false;
    if (Double.doubleToLongBits(maxDuration) != Double
            .doubleToLongBits(other.maxDuration))
        return false;
    if (Double.doubleToLongBits(normalDuration) != Double
            .doubleToLongBits(other.normalDuration))
        return false;
    return true;
}

    public Activity(String label, double d, double e) {
      this.maxDuration = e;
      this.normalDuration = d;
      this.label = label;
}

}

hashCode 和 equals 函数(如上所示)的输入是相同的,函数是确定性的,那么为什么会发生这种情况呢? Activity 对象绝对不可能被更改,因为我明确地将它们设为不可变。所以我完全不知道为什么会发生这种情况。

预先感谢您的任何帮助。

There are two sets which have the same single element.

Set<Activity> a = new HashSet<Activity>();
a.add(new Activity("X", 1, 2));
Set<Activity> b = new HashSet<Activity>();
b.add(new Activity("X", 1, 2));
return a.containsAll(b); //gives false??

the containsAll method returns false when in fact the sets are identical?

I have read answers to similar questions one of which is HashSet contains problem with custom objects, HashSet contains method, strange behavior and I understand how HashSet works. You compute the hash to a bucket and then search for the object using the overridden equals() method.

The activity class is implemented this way:

public class Activity implements Comparable<Activity> {
  private final double maxDuration;
  private final double normalDuration;
  private final String label;   


  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((label == null) ? 0 : label.hashCode());
    long temp;
    temp = Double.doubleToLongBits(maxDuration);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(normalDuration);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
  }

    @Override
   public boolean equals(Object obj) {
      if (this == obj)
        return true;
      if (obj == null)
        return false;
      if (getClass() != obj.getClass())
        return false;
      Activity other = (Activity) obj;
      if (label == null) {
        if (other.label != null)
          return false;
   } else if (!label.equals(other.label))
        return false;
    if (Double.doubleToLongBits(maxDuration) != Double
            .doubleToLongBits(other.maxDuration))
        return false;
    if (Double.doubleToLongBits(normalDuration) != Double
            .doubleToLongBits(other.normalDuration))
        return false;
    return true;
}

    public Activity(String label, double d, double e) {
      this.maxDuration = e;
      this.normalDuration = d;
      this.label = label;
}

}

The inputs to the hashCode and equals functions (as shown above) are the same, the functions are deterministic therefore why does this happen? There is definitely no way that the Activity objects are being changed as I explicitly made them immutable. So I am completely lost as to why this happens.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文