【发布时间】:2011-09-21 07:17:01
【问题描述】:
我需要一个 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在那里两次,它就没有设置。
我还有其他选择吗?
【问题讨论】:
-
这个问题的答案是问stackoverflow.com/questions/5290790/… 的人所追求的。
标签: java collections set concurrent-collections