How to sort CopyOnWriteArrayList

前端 未结 5 1509
名媛妹妹
名媛妹妹 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:37

    Because a CopyOnWriteArrayList copies itself every time you change it, its Iterator doesn't allow you to make changes the list. If it did, the Iterator would not be thread safe, and thread safety is the whole point of this class. Collections.sort() won't work as it requires an Iterator that supports the set() method.

提交回复
热议问题