2020.12.19
//使用iterator遍历集合
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class IteratorDemo {
public static void main(String args[]) {
iterator2List();
iterator2Set();
}
public static void iterator2List() {
List<String> lis = new ArrayList<String>();
lis.add("张三");
lis.add("张三");
lis.add("李四");
Iterator<String> it = lis.iterator();
while (it.hasNext()) {
System obj = it.next();
System.out.println(obj);
}
}
public static void iterator2Set() {
Set<String> set = new HashSet<String>();
set.add("张三");
set.add("张三");
set.add("李四");
set.add("王五");
Iterator<String> it = lis.iterator();
while (it.hasNext()) {
System obj = it.next();
System.out.println(obj);
}
}
}