javacodex.com
Java Examples
Java Examples
memu home questions

Read Elements From A HashSet Using An Iterator

This java example shows how to read all the elements of a HashSet using an iterator.

Source: (HashSetIterator.java)

import java.util.*;
 
public class HashSetIterator {
   public static void main(String args[]){
      HashSet<String> hs = new HashSet<String>();
      hs.add("Bill");
      hs.add("Tom");
      hs.add("Harry");
      hs.add("George");
      Iterator<String> it = hs.iterator();
      while(it.hasNext()) {
         System.out.println(it.next());
      }
   }
}
 

Output:

# java HashSetIterator 
Bill
Harry
Tom
George

Contact: javacodex@yahoo.com