Java的Iterator迭代器

迭代器能够将遍历的操作与序列底层的结构分离

 1 import java.util.*;
 2 
 3 public class CrossContainerIterator {
 4 
 5     public static void main(String[] args) {
 6         
 7         ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(1,2,3));
 8         LinkedList<Integer> linkedList = new LinkedList<Integer>(Arrays.asList(1,2,3));
 9         HashSet<Integer> hashSet = new HashSet<Integer>();
10         
11         hashSet.addAll(Arrays.asList(1,1,3));
12         
13         display(arrayList.iterator());
14         display(linkedList.iterator());
15         display(hashSet.iterator());
16         
17     }
18     public static void display(Iterator<Integer> it)
19     {
20         while(it.hasNext())
21         {
22             System.out.print(it.next());
23         }
24         System.out.println();
25         
26     }
27     
28 
29 }