Java – fail fast iterator

I got this definition: as the name implies, once they realize that the structure of the collection has changed since the beginning of the iteration, the failed fast iterator will fail

What does it mean since the iteration began? This means iterator it = set Iterator ()?

public static void customize(BufferedReader br) throws IOException{  
    Set<String> set=new HashSet<String>(); // Actual type parameter added  
    **Iterator it=set.iterator();**

Solution

First, they fail quickly, not fail safe

Contracts are structural modifications (i.e., insert / delete) to certain types of sets that invalidate existing iterators into the set The failed fast iterator attempts to detect that they should not be valid and throws a concurrentmodificationexception This is provided to you as a service programmer to help you find this type of error faster

In your example:

Iterator it = set.iterator();
it.next();
set.add("unique-entry"); // invalidates the iterator
it.next();

If you are lucky, the second it Next () will detect invalid usage and throw an exception Please note that this is done on a best effort basis and is not guaranteed

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
分享
二维码
< <上一篇
下一篇>>