HashSet parallelStream() in Java

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

Syntax:

Stream<K> java.util.Collection.parallelStream()

This method returns a possibly parallel Stream with this collection as its source. It is allowable for this method to return a sequential stream.

Parameters: NA

Returns: a possibly parallel Stream over the elements in this collection.

Exceptions: NA

Approach

Java

import java.util.HashSet;
import java.util.stream.Stream;

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

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

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

        Stream<String> stream = hashSet.parallelStream();

        stream.forEach(n -> System.out.print(n + " "));
    }
}

Output:

Java C++ Hello 

No comments:

Post a Comment