Related Articles

Related Articles

Traverse through a HashSet in Java
  • Difficulty Level : Basic
  • Last Updated : 11 Dec, 2018

Note that HashSet elements are not ordered, so the traversed elements can be printed in any order.

  1. Using an Iterator : The iterator() method is used to get an iterator over the elements in this set. The elements are returned in no particular order. Below is the java program to demonstrate it.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate iteration over
    // HashSet using an iterator
    import java.util.*;
      
    class IterationDemo {
        public static void main(String[] args)
        {
            HashSet<String> h = new HashSet<String>();
      
            // Adding elements into HashSet usind add()
            h.add("Geeks");
            h.add("for");
            h.add("Geeks");
      
            // Iterating over hash set items
            Iterator<String> i = h.iterator();
            while (i.hasNext())
                System.out.println(i.next());
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    for
    
  2. Using for-each loop :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate iteration over
    // HashSet using an Enhanced for-loop
      
    import java.util.*;
      
    class IterationDemo {
        public static void main(String[] args)
        {
            // your code goes here
            HashSet<String> h = new HashSet<String>();
      
            // Adding elements into HashSet usind add()
            h.add("Geeks");
            h.add("for");
            h.add("Geeks");
      
            // Iterating over hash set items
            for (String i : h) 
                System.out.println(i);        
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    for
    
  3. Using forEach() method :
    In Java 8 or above, we can iterate a List or Collection using forEach() method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate iteration over
    // HashSet using forEach() method
      
    import java.util.*;
      
    class IterationDemo {
        public static void main(String[] args)
        {
            // your code goes here
            HashSet<String> h = new HashSet<String>();
      
            // Adding elements into HashSet usind add()
            h.add("Geeks");
            h.add("for");
            h.add("Geeks");
      
            // Iterating over hash set items
            h.forEach(i -> System.out.println(i));
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    for
    

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :