LinkedHashSet iterator() in Java

iterator(): This method is available in java.util.LinkedHashSet class of Java.

Syntax:

Iterator<K> java.util.HashSet.iterator()

This method returns an iterator over the elements in this set. The elements are returned in no particular order.

Parameters: NA

Returns: an Iterator over the elements in this set.

Exceptions: NA

Approach

Java

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

public class LinkedHashSetiterator {
    public static void main(String[] args) {

        LinkedHashSet<String> linkedHashSet =
new LinkedHashSet<>();

        linkedHashSet.add("Hello");
        linkedHashSet.add("Java");

        Iterator<String> iterator = linkedHashSet.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }

    }
}

Output:

Hello Java 

No comments:

Post a Comment