Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Iterate LinkedHashSet Elements

  • Last Updated : 17 Sep, 2021

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned to the order in which they were inserted.

Different Ways to Iterate LinkedHashSet Elements:

  1. Using the for-each loop
  2. Using iterators
  3. Using JDK 1.8 streams

Method 1: Using the for-each loop

Java




// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashSet
        // Custom inputs
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Iteration over HashSet
        // using for-each loop
        for (Integer element : hashSet)
 
            // Print the elements of LinkedHashSet created above
            System.out.println("Element is " + element);
    }
}
Output
Element is 1
Element is 2
Element is 3
Element is 4
Element is 5

Method 2: Using iterators

Java




// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashSet
        Custom inputs hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Condition check
        // hasNext() method returns true
        // if iterator has more elements to iterate
        // else returns false (where cond violates)
        while (iter.hasNext())
 
            // Print all the elements of LinkedHashSet
            // using next() method
            System.out.println("Element is " + iter.next());
    }
}
Output
Element is 1
Element is 2
Element is 3
Element is 4
Element is 5

Method 3: Using JDK 1.8 streams

Java




// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
   
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashSet
        // Custom inputs
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Stream API used to process collection
        // of objects
 
        // Iterating using for-each
        hashSet.stream().forEach(element -> {
           
            // Print all elements of LinkedHashSet
            System.out.println("Element is  " + element);
        });
    }
}
Output
Element is  1
Element is  2
Element is  3
Element is  4
Element is  5

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!