Different Ways to Iterate over a Set in Java [Snippet]

In this quick article, we will discuss different ways to iterate over a Set in Java. Iterating or looping a Set is a common task in a day to day project work so in this article, I would like to demonstrate different ways to iterate over a Set using JDK 7 and 8.
Set contains only unique elements. It does not allow duplicates. Set also adds a stronger contract on the behavior of equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.
Learn Collections Framework in-depth at Java Collections Tutorial

Java Program to Iterate over a Set

Note that I have used lambda expression and method reference features in this program so you have to use JDK 8 or later to run this program. Please refer the comments in below program are self-descriptive.
package net.javaguides.collections;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Java program to demonstrate different ways to iterate over a Set in Java
 * 
 * @author Ramesh Fadatare
 *
 */
public class DifferentWaysSetIterateProgram {

    public static void main(String...args) {

        Set < String > courses = new HashSet < String > ();
        courses.add("Java");
        courses.add("C");
        courses.add("C++");
        courses.add("Python");
        courses.add("Scala");

        // Enhanced for loop
        for (String course: courses) {
            coursePrinter(course);
        }

        // Basic loop with iterator
        for (Iterator < String > it = courses.iterator(); it.hasNext();) {
            coursePrinter(it.next());
        }

        // While loop with iterator
        Iterator < String > it = courses.iterator();
        while (it.hasNext()) {
            String course = it.next();
            coursePrinter(course);
        }

        // JDK 8 streaming example lambda expression
        courses.stream().forEach(course - > coursePrinter(course));

        // JDK 8 streaming example method reference
        courses.stream().forEach(DifferentWaysSetIterateProgram::coursePrinter);

        // JDK 8 for each with lambda
        courses.forEach(course - > coursePrinter(course));

        // JDK 8 for each
        courses.forEach(DifferentWaysSetIterateProgram::coursePrinter);
    }

    // common method to print course
    private static void coursePrinter(String course) {
        System.out.println("course name :: " + course);
    }
}
Output:
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
course name :: Java
course name :: C++
course name :: C
course name :: Scala
course name :: Python
Please leave a comment if you have any other way to iterate over a Set.

Similar Collections Examples [Snippet]


Comments