Home >Java >javaTutorial >remove trap in java collection iterator Iterator
package TestList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
public class TestIterator {
/**
* @param args
*/
Public static void main(String[] args) {
// TODO Auto-generated method stub
List
list.add("aaa");
list.add("bbb");
list.add("ccc");
Iterator t = list.iterator();
can "bbb")){ //When using iterator to traverse the list collection, if you want to delete an element in any collection, you must delete it when the second to last element is traversed
}
TreeSet< add("fff");
Iterator t1 = set.iterator();
can The elements in a set must be deleted when the first element from the last is traversed
set.remove("eee"); The difference, therefore, causes List and Set to behave differently when deleting collection elements while iterating.