Iterate through elements of HashSet example - Java Collection Framework

Java examples for Collection Framework:HashSet

Description

Iterate through elements of HashSet example

Demo Code


import java.util.HashSet;
import java.util.Iterator;
 
public class Main {
 
  public static void main(String[] args) {
    HashSet hSet = new HashSet();
   //ww w. j  av a  2s  .co m
    hSet.add(new Integer("1"));
    hSet.add(new Integer("2"));
    hSet.add(new Integer("3"));
   
    //get the Iterator
    Iterator itr = hSet.iterator();
   
    System.out.println("HashSet contains : ");
    while(itr.hasNext())
      System.out.println(itr.next());
  }
}

Result


Related Tutorials