Different Ways to Iterate over a Set in Java

Published May 09, 2022

A set is a Java collection that supports value uniqueness. This implies that duplicate entries are not allowed. When two objects are similar, the .equals() function determines that the set may only include one of them.

To Iterate over a set in Java,  there are three methods that you can use. These methods include:

a)     Using Iterator method

b)     Using for loop method

c)      Using the for-each loop method

 

Using Iterator method

In this method, we first use the iterator() method in Java to create an iterator that iterates HashSet. Then we iterate HashSet with hasNext() and Next() in Java. HashSet's hasNext() method checks whether it has elements, and its Next() method accesses its data.

To create an iterator, the following syntax is used:

Iterator<Integer value> it = set.iterator();

 

Example

import java.util.HashSet;

import java.util.Iterator;

public class IterateSetOne {

            public static void main(String[] args) {

        HashSet<Integer> set = new HashSet<>();

        set.add(30);

        set.add(40);

 

        set.add(70);

        set.add(80);

        set.add(90);

        Iterator<Integer> it = set.iterator();

        System.out.println(

            "We have Iterated through the Set : ");

        while (it.hasNext()) {

            System.out.print(it.next() + " ");

        }

            }

}

 

Output:

We have Iterated through the Set :

80 70 40 90 30

 

Using for loop method

A simple for loop is used to iterate HashSet in this method. The following code illustrates this approach:

Example

This example shows how we can create a set and add items and different fruits and then iterate through it. We have added the mango item twice, but the set will not accept duplicates

import java.util.HashSet;

public class Iterate_using_loop {

 

            public static void main(String[] args) {

        HashSet<String> set = new HashSet<>();

        set.add("Mango");

        set.add("Peas");

        set.add("Oranges");

        set.add("Mango");

        set.add("Apples");

        set.add("Berries");

        System.out.println(

            "We have Iterated through a set using loop : ");

        for (String ele : set) {

            System.out.print(ele + " ");

        }

            }

}

 

Output:

We have Iterated through a set using a loop:

Apples Peas Berries Mango Oranges

 

 

Using the for-each loop method

 The Java forEach() loop is used to iterate the HashSet. Here is an example:  

Example

import java.util.HashSet;

public class Iterate_set_using_for_loop {

 

            public static void main(String[] args) {      

        HashSet<String> set = new HashSet<>();

        set.add("Mangoes");

        set.add("Kiwi");

        set.add("berries");

        set.add("Apple");

        set.add("berries");

        System.out.println(

            "We have Iterated a set using forEach loop : ");

        set.forEach(System.out::println);

            }

}

 

Output

We have Iterated a set using a forEach loop :

Apple

Kiwi

Mangoes

berries

 

Conclusion

To conclude, we have learned three methods that we can use to iterate over a set in Java: using an iterator, using a for-loop, and using the for-each loop

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

187 Views