- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
将对象添加到 java.util.TreeSet
时,您希望两个相等的对象只存在一次,当两个对象都被添加时,并且以下测试按预期通过:
@Test
void canAddValueToTreeSetTwice_andSetWillContainOneValue() {
SortedSet<String> sortedSet = new TreeSet<>(Comparator.naturalOrder());
// String created in a silly way to hopefully create two equal Strings that aren't interned
String firstInstance = new String(new char[] {'H', 'e', 'l', 'l', 'o'});
String secondInstance = new String(new char[] {'H', 'e', 'l', 'l', 'o'});
assertThat(firstInstance).isEqualTo(secondInstance);
assertThat(sortedSet.add(firstInstance)).isTrue();
assertThat(sortedSet.add(secondInstance)).isFalse();
assertThat(sortedSet.size()).isEqualTo(1);
}
将这些字符串包装在包装类中,其中 equals()
和 hashCode()
仅基于包装类,但测试失败:
@Test
void canAddWrappedValueToTreeSetTwice_andSetWillContainTwoValues() {
SortedSet<WrappedValue> sortedSet = new TreeSet<>(Comparator.comparing(WrappedValue::getValue).thenComparing(WrappedValue::getCreationTime));
WrappedValue firstInstance = new WrappedValue("Hello");
WrappedValue secondInstance = new WrappedValue("Hello");
assertThat(firstInstance).isEqualTo(secondInstance); // Passes
assertThat(sortedSet.add(firstInstance)).isTrue(); // Passes
assertThat(sortedSet.add(secondInstance)).isFalse(); // Actual: True
assertThat(sortedSet.size()).isEqualTo(1); // Actual: 2
}
private class WrappedValue {
private final String value;
private final long creationTime;
private WrappedValue(String value) {
this.value = value;
this.creationTime = System.nanoTime();
}
private String getValue() {
return value;
}
private long getCreationTime() {
return creationTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WrappedValue)) return false;
WrappedValue that = (WrappedValue) o;
return Objects.equals(this.value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
The JavaDoc for TreeSet.add()
陈述我们的期望:
Adds the specified element to this set if it is not already present. More formally, adds the specified element
e
to this set if the set contains no elemente2
such that(e==null ? e2==null : e.equals(e2))
. If this set already contains the element, the call leaves the set unchanged and returnsfalse
.
鉴于我断言这两个对象是 equal()
,我希望这会通过。我正在假设我遗漏了一些非常明显的东西,除非 TreeSet
不实际使用 Object.equals()
,但在绝大多数情况下使用的东西几乎相同。
这是使用 JDK 1.8.0.60 观察到的 - 我还没有机会测试其他 JDK,但我假设某处存在一些“操作错误”......
最佳答案
问题是给定的用于对集合进行排序的比较器与WrappedValue
的equals
方法不兼容。您期望 SortedSet
表现得像 Set
,但在这种情况下它不会这样做。
来自 SortedSet
:
Note that the ordering maintained by a sorted set [...] must be consistent with
equals
if the sorted set is to correctly implement theSet
interface. [...] This is so because theSet
interface is defined in terms of theequals
operation, but a sorted set performs all element comparisons using itscompareTo
(orcompare
) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of theSet
interface.
换句话说,SortedSet
仅使用您提供的比较器来确定两个元素是否相等。这种情况下的比较器是
Comparator.comparing(WrappedValue::getValue).thenComparing(WrappedValue::getCreationTime)
比较值然后比较创建时间。但是由于 WrappedValue
的构造函数使用 System.nanoTime()
初始化了一个(有效的)唯一创建时间,因此没有两个 WrappedValue
会被认为是相等的这个比较器。因此,就有序集而言
WrappedValue firstInstance = new WrappedValue("Hello");
WrappedValue secondInstance = new WrappedValue("Hello");
是两个不同的对象。事实上,如果您稍微修改构造函数以添加一个 long creationTime
参数,并为两个实例提供相同的时间,您会注意到“预期”结果(即排序集将只有一个添加两个实例后大小为 1)。
所以这里有3种解决方案:
equals
和 hashCode
方法,让它们比较值和时间。SortedSet
在这种特殊情况下的行为不像 Set
。关于java - 为什么我似乎能够将两个彼此相等的对象添加到 TreeSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681505/
如何判断两个TreeSet对象是否相等?我使用open-jdk-10。 ModifiebleObject class ModifiebleObject implements Comparable{
我正在使用 TreeSet 并在调用 TreeSet#add() 方法时发现了 ClassCastException。 代码: public class Testing { public st
假设我有一个自然排序的 TreeSet。我可以使用什么接口(interface)和方法来使新的 TreeSet 保持与第一个列表相同的顺序。 最佳答案 使用相同的Comparator(如果您的元素实现
是否有与 java.util.TreeSet 等效的 VB.NET? 最佳答案 您会发现最接近的是 SortedSet(T) class . 关于treeset - VB.NET 相当于 java.u
我有一个 TreeMap,其中的值是 TreeSet。现在我需要遍历键,对于 TreeSet 的每个元素,我必须删除该元素(然后继续做某事),然后删除该 TreeSet 的第二个元素等。 我试过: f
我今天接受了采访,接受我采访的人对他的陈述感到困惑,询问是否有可能 TreeSet等于 HashSet但不是 HashSet等于 TreeSet .我说“不”,但据他说,答案是"is"。 怎么可能?
这对我来说是一个很深的谜。 看看这个: TreeSet s = new TreeSet(); s.add(Long.valueOf(1)); s.add(Long.valueOf(4)); s.add
如果我想在 Java 的 TreeSet 中删除 log(n) 时间内的最高条目,我使用 treeSet.pollFirst() - Scala 的 mutable.TreeSet 类的等价物是什么?
例如,有一个二叉搜索树,其中包含一系列值。在添加新值之前,我需要检查它是否已经包含它“几乎重复”。我有 Java 解决方案,它只是执行地板和天花板以及完成这项工作的进一步条件。 JAVA : 给定一个
作为最佳实践, float 的集合类型实例不应超过一个。例如,Nil 是 scala 库中的一个 case 对象。 但是, TreeMap 和 TreeSet 在每次 empty() 调用时都会创建一
1、TreeSet 概述 1、TreeSet是 SortedSet 接口的实现类, TreeSet 可以确保集合元素处于排序状态。 2、TreeSet顾名思义他内部维护的是一个TreeMap,
我正在尝试创建一个 TreeSet 来对插入的字符串进行升序排序。我正在使用以下代码在 TreeSet 中输入值。 TreeSet ts = new TreeSet(); ts.add("@Test0
对于此作业,我需要将每个包含 2 个字符串的自定义数据类(称为 User)的实例保存到 TreeSet 中。然后,我必须在我创建的 TreeSet 中搜索从另一个文件的每一行中提取的字符串。第一个文件
好的,我有这个问题要解决: 创建一个名为 VirtualLibrary 的泛型类具有单个属性 totalNumberOfEntries , 以及使用户能够设置和返回条目的方法。条目类型为 Book ,
我不知道如何解释/理解以下有关 TreeSet 和 map 函数的行为。 我想我遗漏了一 block 拼图。任何有关此事的线索都将受到欢迎。 scala> class Person(val name:
我有一个间隔的TreeSet(带有开始和结束的案例类)。如果对此树集进行过滤,例如 treeSet.filter(x => input = x.start) 这预计会在 logN 时间内运行吗? 最佳
我需要一种方法来非常快速地计算整数 TreeSet 中小于 X 的元素数量。 我可以使用 子集() headSet() tailSet() 方法,但它们真的很慢(我只需要计数,而不是数字本身)。有办法
我是java新手,我正在尝试访问与其他类不同的类中的ArrayList和TreeSet。我知道这种从一个类访问它的方式。 这是我拥有 TreeSet 的类(class): public class
这个问题已经有答案了: Java : Comparable vs Comparator [duplicate] (2 个回答) 已关闭 3 年前。 这是我的代码 public class SetTes
我需要保留一个排序的节点列表,从第一个开始,然后获取所有相邻节点。第一个节点和所有其他节点都带有一个种子值,用于根据最低种子值确定接下来将使用哪个节点,一旦一个节点被用于获取相邻节点,它就会被标记为已
我是一名优秀的程序员,十分优秀!