如何实现维护插入顺序的并发Set
我需要一个 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 出现两次则不成立。
我还应该考虑其他选择吗?
I am in the need of a Set implementation that will let me maintain insertion order and still be concuurently modifiable (as in not throw ConcurrentModificationException).
I tried using ConcurrentSkipListSet
with my own comparator - sample code:
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);
}
But it appears this comparator is a #fail because the set prints:
[b, c, a, b, d] . Its no set if b's in there twice.
Are there any other alternatives I should look at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您定义的比较器不遵守总订单属性。对于两个对象,其中一个应该小于另一个,或者另一个小于第一个。
在您的情况下,如果对象不相等,则每个对象都小于另一个对象。
由于您实例化,定义比较器会更容易,因为您会知道您的对象是字符串。
ConcurrentSkipListSet
时没有任何类型参数来说明集合中元素的类型,因此除非使用强制转换,否则定义比较器时会遇到麻烦。但如果您创建一个新的 ConcurrentSkipListSetYou've defined a comparator that does not abide the total order property. For two objects, either should one be smaller than the other, or the other smaller than the first.
In your case, if the objects are not equal, then each is smaller than the other.
Since you're instantiating a
ConcurrentSkipListSet
without any type parameters saying what the type of the elements in the collection will be, you'll have troubles defining the comparator unless you use casting. But if you create anew ConcurrentSkipListSet<String>
, it will be easier to define the comparator, as you will know that your objects are strings.您可以定义一个比较器,它将保持字符串的插入顺序并使用它,它不会很漂亮,但由于总是为每个新元素调用比较器,您需要做的就是这样:
嘿,我说过了不漂亮...
You can define a comparator which will keep the insertion order of the strings and use that, it won't be pretty but since the comparator is always called for each new element all you need to do is something like this:
Hey, I said it wasn't pretty...
我几乎使用了@Asaf的解决方案,但是我对其进行了一些改进,以使其也适用于删除操作:
对于测试:
输出是一个很好且一致的:
[d、b、a、c、g]
[d、a、c、g]
[d, a, g, c]
但我猜 @axel22 对竞争条件的担忧仍然存在。
I pretty much used @Asaf's solution, however I refined it a bit to hold true with remove operations as well:
And for Test:
Output is a nice and consistent:
[d, b, a, c, g]
[d, a, c, g]
[d, a, g, c]
But I guess @axel22 's concern about race condition still holds.