I want to sort CopyOnWriteArrayList
. But when I tried to run the following code
It is throwing unsorted operation exception
.
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.