Java – what happens to its iterator when I sort a list?

Suppose I have a list object and an iterator for that list

Now I use Java util. Collections. Sort() sorts the list

>What happens to iterators? > Is its behavior still defined and can it still be used? > If not, can I prevent breaking iterators in the list?

I know that this problem can be avoided by changing the programming, such as cloning lists, but I specifically want to know the "official" behavior of Java

Solution

java. Most collections in util are "fail fast". If the underlying collection is changed, a concurrentmodificationexception may be thrown It should be pointed out that this is for debugging, so it cannot be guaranteed According to JavaDocs, all endorsements of abstractlist are the same, but copyonwritearraylist is incorrect. It is designed for multithreading

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>