如何实现维护插入顺序的并发Set

发布于 2024-11-16 14:59:28 字数 965 浏览 5 评论 0原文


我需要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-11-23 14:59:28

您定义的比较器不遵守总订单属性。对于两个对象,其中一个应该小于另一个,或者另一个小于第一个。

在您的情况下,如果对象不相等,则每个对象都小于另一个对象。

由于您实例化 ConcurrentSkipListSet 时没有任何类型参数来说明集合中元素的类型,因此除非使用强制转换,否则定义比较器时会遇到麻烦。但如果您创建一个新的 ConcurrentSkipListSet,定义比较器会更容易,因为您会知道您的对象是字符串。

You'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 a new ConcurrentSkipListSet<String>, it will be easier to define the comparator, as you will know that your objects are strings.

乖乖兔^ω^ 2024-11-23 14:59:28

您可以定义一个比较器,它将保持字符串的插入顺序并使用它,它不会很漂亮,但由于总是为每个新元素调用比较器,您需要做的就是这样:

public void testInsertionOrderSkipListSet() {
  Comparator<String> insertionOrderComparator = new Comparator<String>() {

    private final ConcurrentHashMap<String, Integer> order = new ConcurrentHashMap<String, Integer>();
    @Override
    public int compare(String o1, String o2) {
      if (!order.contains(o2)) //only happens on second insert
        order.put(o2, 0);
      if (order.containsKey(o1))
        return order.get(o1).compareTo(order.get(o2));
      order.put(o1, order.size());
      return 1;
    }
  };
  ConcurrentSkipListSet<String> set = new ConcurrentSkipListSet<String>(insertionOrderComparator);

  set.add("a");
  set.add("c");
  set.add("e");
  set.add("b");
  set.add("d");
  set.add("c");
  assertArrayEquals(new String[] { "a", "c", "e", "b", "d"}, set.toArray(new String[]{}));
}

嘿,我说过了不漂亮...

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:

public void testInsertionOrderSkipListSet() {
  Comparator<String> insertionOrderComparator = new Comparator<String>() {

    private final ConcurrentHashMap<String, Integer> order = new ConcurrentHashMap<String, Integer>();
    @Override
    public int compare(String o1, String o2) {
      if (!order.contains(o2)) //only happens on second insert
        order.put(o2, 0);
      if (order.containsKey(o1))
        return order.get(o1).compareTo(order.get(o2));
      order.put(o1, order.size());
      return 1;
    }
  };
  ConcurrentSkipListSet<String> set = new ConcurrentSkipListSet<String>(insertionOrderComparator);

  set.add("a");
  set.add("c");
  set.add("e");
  set.add("b");
  set.add("d");
  set.add("c");
  assertArrayEquals(new String[] { "a", "c", "e", "b", "d"}, set.toArray(new String[]{}));
}

Hey, I said it wasn't pretty...

小瓶盖 2024-11-23 14:59:28

我几乎使用了@Asaf的解决方案,但是我对其进行了一些改进,以使其也适用于删除操作:

class ConcurrentInsertionOrderSet extends ConcurrentSkipListSet{
        Map<Object, Integer> orderMap;
        final AtomicInteger increment = new AtomicInteger();
        public ConcurrentInsertionOrderSet(final Map<Object, Integer> orderMap) {
            super(new Comparator<Object>() {      
                public int compare(Object o1, Object o2) {
                    return (orderMap.get(o1).compareTo(orderMap.get(o2)));
                }
            });
            this.orderMap = orderMap;
        }

        @Override
        public boolean add(Object o) {
            if (!orderMap.containsKey(o)) 
                orderMap.put(o, increment.incrementAndGet());
            return super.add(o);
        }
        @Override
        public boolean remove(Object o) {
            boolean b = super.remove(o);
            if(b)
                orderMap.remove(o);
            return b;
        }
    }

对于测试:

public static void main(String[] str){
        ConcurrentSkipListSet set  = new ConcurrentInsertionOrderSet(new ConcurrentHashMap());
        set.add("d");
        set.add("b");
        set.add("a");
        set.add("c");
        set.add("b");
        set.add("c");
        set.add("g");
        System.out.println(set);
        set.remove("b");
        System.out.println(set);
        set.remove("c");
        set.add("c");
        System.out.println(set);
    }

输出是一个很好且一致的:
[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:

class ConcurrentInsertionOrderSet extends ConcurrentSkipListSet{
        Map<Object, Integer> orderMap;
        final AtomicInteger increment = new AtomicInteger();
        public ConcurrentInsertionOrderSet(final Map<Object, Integer> orderMap) {
            super(new Comparator<Object>() {      
                public int compare(Object o1, Object o2) {
                    return (orderMap.get(o1).compareTo(orderMap.get(o2)));
                }
            });
            this.orderMap = orderMap;
        }

        @Override
        public boolean add(Object o) {
            if (!orderMap.containsKey(o)) 
                orderMap.put(o, increment.incrementAndGet());
            return super.add(o);
        }
        @Override
        public boolean remove(Object o) {
            boolean b = super.remove(o);
            if(b)
                orderMap.remove(o);
            return b;
        }
    }

And for Test:

public static void main(String[] str){
        ConcurrentSkipListSet set  = new ConcurrentInsertionOrderSet(new ConcurrentHashMap());
        set.add("d");
        set.add("b");
        set.add("a");
        set.add("c");
        set.add("b");
        set.add("c");
        set.add("g");
        System.out.println(set);
        set.remove("b");
        System.out.println(set);
        set.remove("c");
        set.add("c");
        System.out.println(set);
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文