我尝试将 Employee 类的对象添加到 TreeSet 中。我没有实现 Comparable 或 Comparator 接口(interface)。但add方法代码在不同的系统中表现不同。为什么这样?代码片段如下:-
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
Set<Employee> set = new TreeSet<Employee>();
set.add(new Employee());
// set.add(new Employee());
// set.add(new Employee());
}
}
在当前系统(Win 10)上,无论我写一次还是三次set.add()方法。它总是在运行时抛出 ClassCastException。 不过说起这个问题——Why does TreeSet throws ClassCastException 那里的用户写道,当他只使用一次 add 方法时,他不会遇到异常。
另外,在另一个系统(Win 7)中,昨天我尝试添加对象3次,调用set方法三次,并且没有ClassCastException!集合的大小仅保持为 1,因此看起来多个对象并未添加到集合中。
那么 add 方法的不同行为的原因可能是什么?
最佳答案
TreeSet.add()
委托(delegate)给 TreeMap.put()
,它在 Java 6 和 Java 8 中具有不同的行为。
Java 6 :
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry<K,V>(key, value, null);
size = 1;
modCount++;
return null;
}
...
Java 8 :
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
...
正如您所看到的,早期版本由于某种原因注释掉了 compare()
行,但在更高版本中又添加了回来。因此,您会看到第一个元素的异常。
另请参阅:Why TreeSet can be used as a key for TreeMap in jdk 1.6?
关于java - 为什么TreeSet的add方法在不同的JRE中表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872948/