
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate through elements of Java LinkedHashSet
LinkedHashSet is a part of Java's Collection Framework. It is a type of Set that keeps the order of elements the same as they were added. It uses a combination of a hash table and a linked list to store elements.
Our task is to traverse/iterate through the elements of a LinkedHashSet in Java. Let's look at some scenarios to understand the problem better:
Scenario 1
Input: {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: The LinkedHashSet contains the elements 1, 2, 3, 4, and 5. When we iterate through it, we get the elements in the order they were inserted.
Scenario 2
Input: {8, 3, 5, 1, 9} Output: 8 3 5 1 9 Explanation: Similar to the previous scenario, the LinkedHashSet contains the elements 8, 3, 5, 1, and 9. When we iterate through it, we get the elements in the order they were inserted.
Iterating through Elements of Java LinkedHashSet
The following are the ways we can iterate through the elements of a LinkedHashSet in Java:
Using a for-each loop
The for-each loop is an enhanced version of the for loop in Java. Using this, we can iterate over each element of collections, arrays, and other iterable objects. Using this, we can directly access each element of the LinkedHashSet without an index or an iterator, as shown below :
LinkedHashSet<Type> set = new LinkedHashSet<>(); for (Type element : set) { // process the element }
Example
In the below example, we will create a LinkedHashSet of Integer type, and then we will iterate through the elements using the for-each loop.
import java.util.LinkedHashSet; public class IterateLinkedHashSet { public static void main(String[] args) { LinkedHashSet<Integer> set = new LinkedHashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); System.out.println("Iterating through the LinkedHashSet:"); for (Integer element : set) { System.out.print(element + " "); } } }
When you run the above code, it will produce the following output:
Iterating through the LinkedHashSet: 1 2 3 4 5
Using an Iterator Object
The Iterator is an interface in Java that provides methods to iterate through collections. It allows us to traverse the elements of a collection one by one. We can get an Iterator object of any collection using the iterator() method.
We can retrieve the Iterator object of a LinkedHashSet and traverse through its contents using the hasNext() and next() methods, as shown below:
LinkedHashSet<Type> set = new LinkedHashSet<>(); Iterator<Type> iterator = set.iterator(); while (iterator.hasNext()) { Type element = iterator.next(); // process the element }
Example
In the below example, we will create a LinkedHashSet, get an iterator object, and iterate through the elements using the hasNetx() and next () methods:
import java.util.Iterator; import java.util.LinkedHashSet; public class IterateLinkedHashSet { public static void main(String[] args) { LinkedHashSet<Integer> set = new LinkedHashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); System.out.println("Iterating through the LinkedHashSet using Iterator:"); Iterator<Integer> iterator = set.iterator(); while (iterator.hasNext()) { Integer element = iterator.next(); System.out.print(element + " "); } } }
When you run the above code, it will produce the following output:
Iterating through the LinkedHashSet using Iterator: 1 2 3 4 5
Using Streams API
Streams API is used to process collections in a more functional way, as it allows chaining operations together. We can use the forEach() method of the Stream interface to iterate through the elements of a LinkedHashSet.
We will use the Streams API to iterate through the elements of a LinkedHashSet. The syntax is as follows:
LinkedHashSet<Type> set = new LinkedHashSet<>(); set.stream().forEach(element -> { // process the element });
Example
In the below example, we will create a LinkedHashSet of Integer type, and then we will create a stream from the LinkedHashSet and use the forEach() method to iterate through the elements.
import java.util.LinkedHashSet; import java.util.stream.Stream; public class IterateLinkedHashSet { public static void main(String[] args) { LinkedHashSet<Integer> set = new LinkedHashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); System.out.println("Iterating through the LinkedHashSet using Streams API:"); Stream<Integer> stream = set.stream(); stream.forEach(element -> System.out.print(element + " ")); } }
When you run the above code, it will produce the following output:
Iterating through the LinkedHashSet using Streams API: 1 2 3 4 5
Conclusion
In this article, we have learned how to iterate through the elements of a LinkedHashSet in Java using three different methods: the for-each loop, the Iterator, and the Streams API.