Iterate over HashSet in Java

The following example shows different ways of iterating over a HashSet
  • Iterate over a HashSet using Java 8 forEach and lambda expression.
  • Iterate over a HashSet using iterator().
  • Iterate over a HashSet using iterator() and Java 8 forEachRemaining() method.
  • Iterate over a HashSet using a simple for-each loop.

Different ways of iterating over a HashSet

package com.javaguides.collections.hashsetexamples;

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

public class IterateOverHashSetExample {
    public static void main(String[] args) {
        Set < String > programmingLanguages = new HashSet < > ();
        programmingLanguages.add("C");
        programmingLanguages.add("C++");
        programmingLanguages.add("Java");
        programmingLanguages.add("Python");
        programmingLanguages.add("PHP");
        programmingLanguages.add("Ruby");

        System.out.println("=== Iterate over a HashSet using Java 8 forEach and lambda ===");
        programmingLanguages.forEach(programmingLanguage - > {
            System.out.println(programmingLanguage);
        });

        System.out.println("=== Iterate over a HashSet using iterator() ===");
        Iterator < String > programmingLanguageIterator = programmingLanguages.iterator();
        while (programmingLanguageIterator.hasNext()) {
            String programmingLanguage = programmingLanguageIterator.next();
            System.out.println(programmingLanguage);
        }

        System.out.println("=== Iterate over a HashSet using iterator() and Java 8 forEachRemaining() method ===");
        programmingLanguageIterator = programmingLanguages.iterator();
        programmingLanguageIterator.forEachRemaining(programmingLanguage - > {
            System.out.println(programmingLanguage);
        });

        System.out.println("=== Iterate over a HashSet using simple for-each loop ===");
        for (String programmingLanguage: programmingLanguages) {
            System.out.println(programmingLanguage);
        }
    }
}

Output

=== Iterate over a HashSet using Java 8 forEach and lambda ===
Java
C++
C
PHP
Ruby
Python
=== Iterate over a HashSet using iterator() ===
Java
C++
C
PHP
Ruby
Python
=== Iterate over a HashSet using iterator() and Java 8 forEachRemaining() method ===
Java
C++
C
PHP
Ruby
Python
=== Iterate over a HashSet using simple for-each loop ===
Java
C++
C
PHP
Ruby
Python

Related Java HashSet Source Code Examples


Comments