How to Iterate Set in Java

In this tutorial, we will learn how to iterate Set in Java. Basically, Iteration means repeating the same operation multiple times.

 

Set interface does not provide any get() method like List to retrieve elements. Therefore, the only way to take out elements from set is to do using Iterator() method but this method does not return elements from 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 iterator method. 

 

List can be iterated by using ListIterator method but Set cannot be iterated by using ListIterator. Let’s see in detail with example programs.


There are three ways to iterate set in Java.

1. Using Iterator
2. Using Enhanced for loop
3. Using forEach() 
Ways to iterate set in java

How to iterate Set using Iterator in Java?


Let’s take an example program where we will iterate elements of set using iterator() method. Follow all the steps in the coding.
Program source code 1:
package iterateSet; 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.Set; 
public class IterateSetEx 
{ 
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 set. 
   s.add("Orange"); 
   s.add("Red"); 
   s.add("Blue"); 
   s.add("Yellow"); 
   s.add("Green"); 
System.out.println("Elements in 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 set 
       [Red, Blue, Yellow, Orange, Green] 
       Iteration using Iterator method 
       Red Blue Yellow Orange Green

How to iterate Set using Enhanced For loop?


Let’s make a program where we will iterate elements of set using enhanced for loop. 
Program source code 2:
package iterateSet; 
import java.util.HashSet; 
import java.util.Set; 
public class IterateSetEx2 
{ 
public static void main(String[] args) 
{ 
// Create 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 iterateSet; 
import java.util.HashSet; 
import java.util.Set; 
public class IterateSetEx3 
{ 
public static void main(String[] args) 
{ 
 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 loop in Java 1.8"); 
 s.forEach(System.out::println); 
 } 
}
Output: 
       [A, B, C, D, E] 
       Iterating using forEach loop in Java 1.8 
       A B C D E
Note: forEach() method is available from Java 1.8 onwards. 

Final words 
Hope that this tutorial has covered various types of example programs based on iterating set in Java. I hope that you will have understood this topic and enjoyed them.
Thanks for reading!!! 

Next ⇒ Java HashSet⇐ PrevNext ⇒

Leave a Comment