HashSet spliterator() in Java

spliterator(): This method is available in java.util.HashSet class of Java.

Syntax:

Spliterator<K> java.util.HashSet.spliterator()

This method creates a late-binding and fail-fast Spliterator over the elements in this set.

Parameters: NA

Returns: a Spliterator over the elements in this set.

Exceptions: NA

Approach

Java

import java.util.HashSet;
import java.util.Spliterator;

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

        HashSet<String> hashSet = new HashSet<String>();

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

        Spliterator<String> spliterator = hashSet.spliterator();

        spliterator.forEachRemaining(n ->
System.out.print(n + " "));

    }
}

Output:

Java Hello 

No comments:

Post a Comment