EnumSet parallelStream() in Java

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

Syntax:

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

This method returns a possibly parallel Stream with this collection as its source.

Note: 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.EnumSet;
import java.util.stream.Stream;

public class EnumSetparallelStream {
    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumSet<Colour> enumSet = EnumSet.allOf(Colour.class);

        Stream<Colour> stream = enumSet.parallelStream();

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

    }
}

Output:

YELLOW ORANGE GREEN RED 


No comments:

Post a Comment