Iterating over a HashSet using Iterator

The following example shows different ways of iterating over a HashSet using Iterator:
  • Iterate over a HashSet using iterator().
  • Iterate over a HashSet using iterator() and Java 8 forEachRemaining() method.

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 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);
        });
    }
}

Output

=== 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


Comments