Java DoubleStream parallel()
DoubleStream parallel() 是 java.util.stream.DoubleStream 中的一个方法。该方法返回一个并行的DoubleStream,也就是说,它可能会返回自己,这是因为流已经存在,或者是因为底层的流状态被修改成了并行。
DoubleStream parallel()是一个 中间操作。 这些操作始终是懒惰的。中间操作是在一个流实例上调用的,在它们完成处理后,会给出一个流实例作为输出:
语法:
DoubleStream parallel()
其中,DoubleStream是一串原始的
元素的序列,并且该函数
返回一个并行的DoubleStream。
例1 :
// Java program to demonstrate working of
// DoubleStream parallel() on a given range
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a stream of doubles
DoubleStream stream =
DoubleStream.of(5.2, 12.6, 4.5, 7.8);
System.out.println("The corresponding "
+ "parallel DoubleStream is :");
stream.parallel().forEach(System.out::println);
}
}
输出:
The corresponding parallel DoubleStream is :
4.5
7.8
12.6
5.2
例2 :
// Printing sequential stream for the
// same input as above example 1.
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a stream of doubles
DoubleStream stream =
DoubleStream.of(5.2, 12.6, 4.5, 7.8);
System.out.println("The corresponding "
+ "sequential DoubleStream is :");
stream.sequential().forEach(System.out::println);
}
}
输出:
The corresponding sequential DoubleStream is :
5.2
12.6
4.5
7.8
例3 :
// Java program to show sorted output
// of parallel stream.
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a stream of doubles
DoubleStream stream =
DoubleStream.of(2.3, 3.0, 4.5, 6.6);
System.out.println("The sorted parallel"
+ " DoubleStream is :");
stream.parallel().sorted().forEach(System.out::println);
}
}
输出:
The sorted parallel DoubleStream is :
4.5
6.6
2.3
3.0