Article From:https://www.cnblogs.com/xiuxiu1105/p/9911845.html

1.The ergodic principle of iterators,
First, let’s talk about the iteration principle of iterators. Here we take the set as an example. Typically, iterators in a collection can be used as a way to traverse elements in a collection. The following code gets the iterator through the list. Iterator () method of the collection, and then goes into the while loop to execute it.The it. hasNext () statement determines whether the next object in the collection exists and exists in a loop. Execute it. nest () to get the next object, and then go back to it. hasNext ()…. Knowing that it. hasNext () judges that there is no next objectEnd the loop. In particular, the enhanced for in the set, which is the bottom of the foreach loop, is also an iterator loop.


Iterator it=list.iterator(); while(it.hasNext()){ System.out.println(it.next()); }

2.Iterator memory analysis, through the iterator deletion object can ensure synchronization of two threads (you can see the source code of the iterator deletion method and the set deletion method)

   

              In summary, we can’t use the remove method of the set to delete in the iteration process. We can only use it. remove () of the iterator to delete.

Leave a Reply

Your email address will not be published. Required fields are marked *