用户登录
用户注册

分享至

HashSet 包含重复的条目

  • 作者: 一叶孤城17796060
  • 来源: 51数据库
  • 2022-10-26

问题描述

当 equals 方法说它们相同时,HashSet 只存储值.我就是这么想的.

A HashSet only stores values ones, when the equals method says that they're the same. Thats what I thought.

但是现在我将元素添加到 HashSet 中,其中 equals 方法返回 true 并且集合的大小仍在增长?对不起,我很困惑.一些我错了的提示会很好.

But now i'm adding Elements to a HashSet where the equals method returns true and the size of the set still grows?? sorry I'm confused. Some hints where i'm wrong would be nice.

Element t1 = new Element(false, false, false, false);
Element t2 = new Element(true, true, true, true);
Element t3 = new Element(false, false, false, false);

if (t1.equals(t3))
    System.out.println("they're equal");

Set<Element> set = new HashSet<>();

set.add(t1);
set.add(t2);
set.add(t3);

System.out.println("set size: " + set.size());

所以在这个例子中我的控制台输出是:

so in this example my console output is:

他们是平等的
设置大小:3

they're equal
set size: 3

这对我来说毫无意义.. 大小应该是 2 吗?

That makes no sense to me.. shouldn the size be 2?

推荐答案

问题是你的 Element 类没有覆盖 equals 和 hashCode 方法或这些实现被破坏了.

The problem is that your Element class has not overridden the equals and hashCode methods or these implementations are broken.

来自 对象#equals 方法 javadoc:

From Object#equals method javadoc:

equals 方法在非空对象引用上实现等价关系:

The equals method implements an equivalence relation on non-null object references:

  • 它是自反的:对于任何非空引用值 x,x.equals(x) 应该返回 true.
  • 它是对称的:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应该返回 true.
  • 它是可传递的:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true 并且 y.equals(z) 返回 true,则 x.equals(z) 应该返回 true.这是一致的:对于任何非空引用值 x 和 y,-x.equals(y) 的多次调用始终返回 true 或始终返回 false,前提是没有修改对象上 equals 比较中使用的信息.
  • 对于任何非空引用值 x,x.equals(null) 应该返回 false.

来自 Object#hashCode 方法javadoc:

From Object#hashCode method javadoc:

hashCode的一般合约是:

The general contract of hashCode is:

  • 只要在 Java 应用程序执行期间对同一个对象多次调用,hashCode 方法必须始终返回相同的整数,前提是没有修改对象上的 equals 比较中使用的信息.该整数不需要在应用程序的一次执行与同一应用程序的另一次执行之间保持一致.
  • 如果根据 equals(Object) 方法,两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果.
  • 不要求如果两个对象根据equals(java.lang.Object)方法不相等,那么对两个对象中的每一个调用hashCode方法必须产生不同的整数结果.但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能.

确保这些方法的实现满足这些规则,并且您的 Set(由 HashSet 支持)将按预期工作.

Make sure the implementations of these methods satisfy these rules and your Set (backed by a HashSet) will work as expected.

软件
前端设计
程序设计
Java相关