我需要一个 Set 实现,它可以让我保持插入顺序并且仍然可以修改(如不抛出 ConcurrentModificationException)。
我尝试使用ConcurrentSkipListSet
我自己的比较器 - 示例代码:
public static void main(String[] str){
ConcurrentSkipListSet set = new ConcurrentSkipListSet(new Comparator() {
public int compare(Object o1, Object o2) {
if(o1.equals(o2)){
return 0;
}
return -1;
}
});
set.add("d");
set.add("b");
set.add("a");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
set.add("c");
set.add("b");
System.out.println(set);
set.remove("b");
System.out.println(set);
}
但看起来这个比较器是一个 #fail 因为集合打印:
[b, c, a, b, d] 。如果b在那里两次,它就没有设置。
我还有其他选择吗?