Import Java. util. collection; import Java. util. hashset; import Java. util. iterator; // The interator interface traverses the array set public class testiterator {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub // create a set // The hashset is unordered and the data in it is not repeated, collection <string> colls = new hashset <string> (); colls. add ("first"); colls. add ("second"); colls. add ("third ");
Colls. Add ("third ");
If one output is added in this way, although two new objects are added, the actual results are returned when equle is used for comparison.
// Colls. Ass (new string ("hello "));
// Colls. Ass (new string ("hello "));
Colls. add ("4"); colls. add ("5"); colls. add ("6"); colls. add ("7"); colls. add ("8"); // get colls iterator <string> I = colls. iterator (); // iteration while (I. hasnext () {string M = I. next (); system. out. println (m);} system. out. println (colls); system. out. println ("Total" + colls. size () + "element ");}}
The output structure is:
View code
Second 7 third 6548 first [second, 7, third, 6, 5, 4, 8, first] A total of 8 Elements
We can also see that the storage of hashset is unordered.