Java - TreeSet 和 hashCode()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:39:34  来源:igfitidea点击:

Java - TreeSet and hashCode()

javahashcodetreeset

提问by Gaz

I have a quick question about TreeSetcollections and hashCodemethods. I have a TreeSetand I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSetusing the containsmethod.

我有一个关于TreeSet集合和hashCode方法的快速问题。我有一个TreeSet,我正在向它添加对象,在添加对象之前,我检查它是否存在于TreeSetusingcontains方法中。

I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below:

我有 2 个不同的对象,每个对象使用我的 hashCode 方法实现生成一个不同的 hashCode,示例如下:

public int hashCode()
{
    int hash = 7;
    hash = hash * 31 + anAttribute.hashCode();
    hash = hash * 31 + anotherAttribute.hashCode();
    hash = hash * 31 + yetAnotherAttribute.hashCode();
    return hash;
}

The hashCodes for a particular run are: 76126352 and 76126353 (the objects only differ by one digit in one attribute).

特定运行的哈希码为:76126352 和 76126353(对象在一个属性中仅相差一位)。

The contains method is returning true for these objects, even though the hashCodes are different. Any ideas why? This is really confusing and help would really be appreciated.

即使 hashCode 不同,contains 方法也会为这些对象返回 true。任何想法为什么?这真的很令人困惑,我们将不胜感激。

回答by sepp2k

TreeSet does not use hashCodeat all. It uses either compareToor the Comparator you passed to the constructor. This is used by methods like contains to find objects in the set.

TreeSet 根本不使用hashCode。它使用compareTo或 您传递给构造函数的比较器。这被 contains 之类的方法用于在集合中查找对象。

So the answer to your question is that your compareTo method or your Comparator are defined so that the two objects in question are considered equal.

所以你的问题的答案是你的 compareTo 方法或你的 Comparator 被定义为两个有问题的对象被认为是相等的。

From the javadocs:

从javadocs:

a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.

TreeSet 实例使用它的 compareTo(或 compare)方法执行所有元素比较,因此从集合的角度来看,被此方法视为相等的两个元素是相等的。

回答by tuergeist

From Java Doc:

来自 Java 文档:

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

如果根据 equals(Object) 方法两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。

Means: the objects you use for hashing are not equal.

意味着:您用于散列的对象不相等。

回答by duffymo

You need to read Joshua Bloch's "Effective Java" chapter 3. It explains the equals contract and how to properly override equals, hashCode, and compareTo.

您需要阅读 Joshua Bloch 的“Effective Java”第 3 章。它解释了 equals 约定以及如何正确覆盖 equals、hashCode 和 compareTo。

回答by helpermethod

You don't need to checked if it is contained, because the insert() basically does the same operation (i.e. searching the proper position) on its way to the insertion point. If the object can't be inserted (i.e., the object is already contained), insert returns false.

您不需要检查它是否被包含,因为 insert() 在到达插入点的途中基本上执行相同的操作(即搜索正确的位置)。如果无法插入对象(即对象已包含),则插入返回 false。