Iterate over LinkedHashSet in Java Example

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

Different ways of iterating over a LinkedHashSet

package com.javaguides.collections.linkedhashsetexamples;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

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

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

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

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

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

Output

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

Reference

Comments