java - TreeSet 等于另一个 TreeSet
问题描述
如何判断两个TreeSet
对象是否相等?我使用open-jdk-10。
可修改对象
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);
}
更正式地说,当且仅当这个集合包含一个元素 {@code e} 使得 {@code Objects.equals(o, e)} 时才返回 {@code true}。
但实际上它使用compareTo
.
更新
我可以使用来自 jdk 或其他库的其他集合来保证唯一元素并排序,并且等于另一个集合用于每个元素 equals。
解决方案
如果您有一组定义的对象,那么您可以覆盖哈希码和等于方法以及基于哈希码和等于方法的以下方法比较。您可以使用
org.apache.commons.collections
SetUtils.isEqualSet(set1, set2);
或者
org.apache.commons.collections
CollectionUtils.isEqualCollection(a, b)
推荐阅读
- python - 为什么 python opencv 只从我的网络摄像头读取第一帧?
- javascript - 从一个输入修改条带结帐表单包含多个输入所需的所有数据
- ruby-on-rails - 在 Arel 中重写 ActiveRecord 查询
- android - 应用内结算。如果用户在“付款成功”对话框未关闭时关闭了应用程序
- gremlin - AWS Neptune 类型转换
- typescript - 如何创建此 TypeScript 接口的函数实例?
- npm - 在 npm 包中嵌入 Emscripten WebAssembly 模块
- ios - 是否可以使用 websockets 在 iPhone 和服务器之间实现 50,000 个活动连接?
- java - Maven 包 An Ant BuildException has occurred: Invalid license info
- c# - 在wpf xaml中单击时获取列表框项目的名称