1.ArrayList
1 List list = new ArrayList<Integer>(); 2 list.add(1); 3 list.add(2); 4 list.add(3); 5 // 创建一个迭代器 6 Iterator iter = list.iterator(); 7 // 循环迭代 8 while(iter.hasNext()){ 9 Integer i =(Integer) iter.next(); 10 System.out.println(i); 11 }
2.Set集合
1 Set set = new HashSet<>(); 2 //存储数据 3 set.add(new Person(1,"张三",20)); 4 set.add(new Person(2,"李四",30)); 5 set.add(new Person(3,"王五",40)); 6 7 //遍历数据 8 Iterator iter = set.iterator(); 9 while(iter.hasNext()){ 10 Person person = (Person)iter.next(); 11 System.out.println(person.getId() + person.getName() + person.getAge()); 12 }
3.Map集合
1 Map map = new HashMap<>(); 2 map.put(1, "a"); 3 map.put(2, "b"); 4 map.put(3, "c"); 5 6 Set set = map.keySet(); 7 System.out.println(set); 8 Collection col=map.values(); 9 System.out.println(col); 10 11 Set<Map.Entry<Integer, String>> entrySet=map.entrySet(); 12 Iterator<Map.Entry<Integer, String>> iter=entrySet.iterator(); 13 while(iter.hasNext()) { 14 Map.Entry<Integer, String> entry=iter.next(); 15 System.out.println("键:"+entry.getKey()); 16 System.out.println("值:"+entry.getValue()); 17 } 18