How to Iterate through elements of HashSet in Java

1 Answer

0 votes
package javaapplication1;

import java.util.HashSet;
import java.util.Iterator;

public class JavaApplication1 {

    public static void main(String[] args) {

        HashSet hash = new HashSet();
   
        hash.add(new Integer("1"));
        hash.add(new Integer("2"));
        hash.add(new Integer("3"));
        hash.add(new Integer("4"));
        hash.add(new Integer("5"));
        hash.add(new Integer("6"));
        hash.add(new Integer("7"));

        Iterator itr = hash.iterator();

        while (itr.hasNext())
            System.out.println(itr.next());
  }
}
  
/*
run:

1
2
3
4
5
6
7

*/

 



answered Sep 26, 2016 by avibootz

Related questions

...