. 1 Package cn.learn.collection; 2 . 3 Import java.util.Collection; . 4 Import java.util.HashSet with; . 5 Import the java.util.Iterator; . 6 . 7 / * . 8 the java.util.Iterator . 9 iterator: mainly used collection visit traversal, traversal will first determine whether the set is empty 10 . 11 objects iterator iterator is also referred to as 12 is iterator is an interface that requires the use of the class object implement iterator interface 13 acquires special methods, there are a collection iterator ( ) method returns an Iterator implementation class 14 15 first () determine whether the element with the hasNext, returns a Boolean, then the Next (), remove the element 16 * / . 17 public class{IteratorApi 18 is public static void main (String [] args) { . 19 HS = Collection <String> new new HashSet <> (); 20 is hs.add ( "Ni" ); 21 is hs.add ( "NN" ); 22 is HS .add ( "N1" ); 23 is hs.add ( "N1" ); 24 hs.add ( "N2" ); 25 System.out.println (HS); // [NN, N1, N2, Ni] 26 is // the interface implementation class pointed, multi-state, Iterator <E> is generic to go along with a collection of 27 // ITER index set point is -1, to the hasNext (); 28 ITER = iterator hs.iterator (); 29 // iterates over 30 the while (iter.hasNext ()) { 31 is // iter.next () does two things, 1: Remove the element 2: the pointer moves 32 the System .out.println (iter.next ()); 33 is } 34 is System.out.println (iter.hasNext ()); // to false 35 36 37 [ / * 38 is enhanced for loop, after JDK1.5, designed to traverse and not to add or delete operation 39 format: 40 for (data type variables: name can not be set or array iterator name) { 41 // operation code 42 } 43 * / 44 //The default number of times for loop size is set or array (); Iterator as internal principles and 45 for (String STR: HS) { 46 is // for (Iterator <String> = hs.iterator Iterator (); iterator.hasNext () ;) 47 System.out.println (STR); 48 } 49 50 } 51 is }