How to sort CopyOnWriteArrayList

前端 未结 5 1493
名媛妹妹
名媛妹妹 2021-02-06 08:46

I want to sort CopyOnWriteArrayList. But when I tried to run the following code

It is throwing unsorted operation exception.



        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 09:16

    In JDK1.8 can use sort(Comparator c) directly.

    List list = new CopyOnWriteArrayList();
    
    list.add(3);
    list.add(4);
    list.add(1);
    
    list.sort(new Comparator() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    });
    

提交回复
热议问题