
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
How to iterate HashSet in Java?
The HashSet class is a part of the Java Collections Framework, and it implements the Set interface. It is a collection of unique individual elements. It internally uses a hash table; therefore, the iteration order of elements is not guaranteed.
Our task is to iterate through the elements of a HashSet 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 HashSet contains the elements 1, 2, 3, 4
Scenario 2
Input: {8, 3, 5, 1, 9} Output: 8 3 5 1 9 Explanation: The HashSet contains the elements 8, 3, 5, 1, and 9. When we iterate through it, we get the elements in no specific order.
Iterating through Java HashSet
The following are the ways we can iterate through the elements of a HashSet in Java:
Using a for-each loop
The for-each loop is an enhanced version of the for loop in Java. It is used for iterating over collections, arrays, and other iterable objects.
Using the for-each loop, we can directly access each element of the HashSet without using an index or an iterator.
Example
In the following example, we will create a HashSet of Integer type, and then we will iterate through the elements using the for-each loop:
import java.util.HashSet; import java.util.Set; public class IterateHashSet { public static void main(String[] args) { Set<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.add(2); hashSet.add(3); hashSet.add(4); hashSet.add(5); System.out.println("Iterating through the HashSet:"); for (Integer element : hashSet) { System.out.print(element + " "); } } }
When you run the above code, it will produce the following output:
Iterating through the HashSet: 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 use the hasNext() and next() methods of the Iterator interface to iterate through the elements of a HashSet.
Syntax
The following is the syntax of using an Iterator to iterate through a HashSet:
Iterator<Integer> it = set.iterator();
Example
In the following example, we are accessing the elements of a HashSet:
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class IterateHashSet { public static void main(String[] args) { Set<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.add(2); hashSet.add(3); hashSet.add(4); hashSet.add(5); System.out.println("Iterating through the HashSet using Iterator:"); Iterator<Integer> it = hashSet.iterator(); while (it.hasNext()) { System.out.print(it.next() + " "); } } }
When you run the above code, it will produce the following output:
Iterating through the HashSet using Iterator: 1 2 3 4 5
Using Streams API
Streams API is a part of Java 8 and provides a functional approach to processing collections. We can use the forEach() method of the Stream interface to iterate through the elements of a HashSet.
Example
In the below example, we will create a HashSet of Integer type, and then we will create a stream from the HashSet using the stream() method, and then we will use the forEach method to iterate over it.
import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; public class IterateHashSet { public static void main(String[] args) { Set<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.add(2); hashSet.add(3); hashSet.add(4); hashSet.add(5); System.out.println("Iterating through the HashSet using Streams:"); Stream<Integer> stream = hashSet.stream(); stream.forEach(element -> System.out.print(element + " ")); } }
When you run the above code, it will produce the following output:
Iterating through the HashSet using Streams: 1 2 3 4 5
In this article, we have discussed how to iterate through a HashSet in Java using different methods such as for-each loop, Iterator, and Streams API.