gpt4 book ai didi

java - TreeSet 等于另一个 TreeSet

转载 作者:行者123 更新时间:2023-12-01 17:46:29 28 4
gpt4 key购买 nike

如何判断两个TreeSet对象是否相等?我使用open-jdk-10

ModifiebleObject

class ModifiebleObject implements Comparable<ModifiebleObject>{

Integer number;
String text;

@Override
public int compareTo(final ModifiebleObject o) {
return this.number - o.number;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof ModifiebleObject)) return false;
final ModifiebleObject that = (ModifiebleObject) o;
return Objects.equals(number, that.number) &&
Objects.equals(text, that.text);
}

@Override
public int hashCode() {
return Objects.hash(number, text);
}
}

一些代码

SortedSet<ModifiebleObject> tree1 = prepare();
SortedSet<ModifiebleObject> tree2 = prepare(); //Returns cloned elements, so object references in tree1 and tree2 are different.

// ModifiebleObject implements Comparable<ModifiebleObject>
// compareTo does not use all the fields, just some of them.
//setSomeValueOutsideOfComparable sets value of the field, which is not used by compareTo
tree2.first().setSomeValueOutsideOfComparable("newValue");

boolean tree1EqualsTree2 = tree1.equals(tree2); //Returns true

因为

TreeSet 调用 AbstractSet.containsAll -> TreeSet.contains -> TreeMap.containsKey -> TreeMap.getEntry != null

TreeMap.getEntry 使用压缩器或元素compareTo(元素实现Comparable)。

有趣,但 JavaDoc 撒谎了!

java.utilTreeSet

/**
* Returns {@code true} if this set contains the specified element.
* More formally, returns {@code true} if and only if this set
* contains an element {@code e} such that
* {@code Objects.equals(o, e)}.
*
* @param o object to be checked for containment in this set
* @return {@code true} if this set contains the specified element
* @throws ClassCastException if the specified object cannot be compared
* with the elements currently in the set
* @throws NullPointerException if the specified element is null
* and this set uses natural ordering, or its comparator
* does not permit null elements
*/
public boolean contains(Object o) {
return m.containsKey(o);
}

More formally, returns {@code true} if and only if this set contains an element {@code e} such that {@code Objects.equals(o, e)}.

但实际上它使用的是compareTo

<小时/>

更新

我可以使用jdk或其他库中的哪些其他集合来保证元素的唯一性和排序,以及等于另一个集合对每个元素使用的等于。

最佳答案

JavaDoc for TreeSet明确地说了这一点。这不是什么阴谋。

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but 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. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

你的类(class)忽略了the advice of Comparable并且您正在为此付出代价。

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))

关于java - TreeSet 等于另一个 TreeSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54592253/

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