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<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
Iterator t = list.iterator();
while(t.hasNext()){
if(t.next().equals("bbb")){ //When using iterator to traverse list collection, if you want to delete elements in any collection, you must delete them when it just traversing the second to last element.
list.remove("ccc");
}
}
TreeSet<String> set = new TreeSet<String>();
set.add("ddd");
set.add("eee");
set.add("ffff");
Iterator t1 = set.iterator();
while(t1.hasNext()){
if(t1.next().equals("fff")){ //When using iterator to traverse the set set, if you want to delete elements in any set, you must delete them when it just traversing the last element.
set.remove("ee");
}
}
}
}
Since there are certain differences in List and Set when implementing Iterator, the list and Set will have different performance when deleting collection elements while iterating.