Scientech Easy How to Iterate a Set in Java | Scientech Easy

Monday, December 31, 2018

How to Iterate a Set in Java

In this tutorial, we will learn how to iterate a Set in Java. Basically, Iteration means repeating the same operation multiple times. Since the Set interface does not provide any get() method like List interface to retrieve the elements, Therefore, the only way to take out elements from a set is to do using an Iterator() method but this method does not return the elements from the set in any particular order. Using Iterator, we can traverse only in the forward direction from the first to last element. We cannot traverse over elements in the backward direction using the iterator method. 
Since List can be iterated by using ListIterator method but Set cannot be iterated by using ListIterator. Let's see in detail with practical example programs

How to Iterate a Set in Java


There are following three ways to iterate a set in Java.
1. Using Iterator
2. Using Enhanced for loop
3. Using forEach() 
Iterate Set in Java, Best way to iterate Set in Java

How to iterate Set using Iterator in Java


Program source code 1:
    package setPractice; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class IterateSetExample { public static void main(String[] args) { // Create a generic set object of type string. Set<String> s=new HashSet<String>(); int size=s.size(); System.out.println("Size before adding elements: " +size); // Adding elements to the set. s.add("Orange"); s.add("Red"); s.add("Blue"); s.add("Yellow"); s.add("Green"); System.out.println("Elements in the set"); System.out.println(s); Iterator<String> itr=s.iterator(); System.out.println("Iteration using iterator method"); while(itr.hasNext()){ Object str=itr.next(); System.out.println(str); } } }
    Output: Size before adding elements: 0 Elements in the set [Red, Blue, Yellow, Orange, Green] Iteration using iterator method Red Blue Yellow Orange Green

How to iterate Set using Enhanced For loop


Program source code 2:
    package setPractice; import java.util.HashSet; import java.util.Set; public class IterateSetExample2 { public static void main(String[] args) { // Create a Set object of type Integer. Set<Integer> s=new HashSet<Integer>(); // Adding even numbers betwen 10 to 30 as elements. for(int i=10; i<=30; i++){ if(i%2==0){ s.add(i); } } System.out.println("Even numbers between 10 to 30"); System.out.println(s); System.out.println("Iteration Using Enchanced For Loop"); for(Integer it:s){ System.out.println(it); } } }
    Output: Even numbers between 10 to 30 [16, 18, 20, 22, 24, 10, 26, 12, 28, 14, 30] Iteration Using Enhanced For Loop 16 18 20 22 24 10 26 12 28 14 30

How iterate Set using forEach loop in Java 1.8


Program source code 3:
    package setPractice; import java.util.HashSet; import java.util.Set; public class IterateSetExample3 { public static void main(String[] args) { // Create a Set object of type Integer. Set<Character> s=new HashSet<Character>(); s.add('A'); s.add('B'); s.add('C'); s.add('D'); s.add('E'); System.out.println(s); System.out.println("Iterating using forEach() method in Java 1.8"); s.forEach(System.out::println); } }
    Output: [A, B, C, D, E] Iterating using forEach() method in Java 1.8 A B C D E
Note: forEach() method is available from Java 1.8 onwards. 

Final words 
We hope that this article has covered the practical example programs for iterating a Set in Java. All example programs are important for beginners. So practice them to understand the concepts.