Java LinkedHashSet Iterate Example

This example shows how to iterate over Java LinkedHashSet elements. The example also shows how to iterate through LinkedHashSet elements using for loop, Iterator, and forEach method.

How to iterate LinkedHashSet in Java?

There are several ways using which we can iterate through LinkedHashSet elements in Java as given below.

1. Iterate using the for loop

We can use the enhanced for loop to iterate over elements of the LinkedHashSet objects as given below.


2. Iterate using the Iterator

We can get an iterator over the LinkedHashSet elements using the iterator method.

Once we get an Iterator for the linked hash set object, we can use the hasNext method and the next method along with the while loop to iterate through its elements.


3. Iterate using the forEach method

If you are using Java version 8 or later, you can use the forEach method of the Iterables to iterate over LinkedHashSet elements as given below.



Unlike the HashSet class in Java, the LinkedHashSet class maintains a doubly-linked list running through all of its elementsĀ and guarantees the order of elements. It means you will get the elements in the same order in which they were inserted using any of the above given methods.

This example is a part of the Java LinkedHashSet Tutorial with Examples.

Please let me know your views in the comments section below.

Java 8 LinkedHashSet

About the author



My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping