📜  Java中的DoubleStream并行()

📅  最后修改于: 2022-05-13 01:55:33.575000             🧑  作者: Mango

Java中的DoubleStream并行()

DoubleStream parallel()是Java.util.stream.DoubleStream 中的一个方法。此方法返回一个并行的 DoubleStream,即它可能返回自身,因为流已经存在,或者因为底层流状态被修改为并行。

DoubleStream parallel() 是一个中间操作。这些操作总是懒惰的。在 Stream 实例上调用中间操作,在它们完成处理后,它们给出一个 Stream 实例作为输出。

句法 :

DoubleStream parallel()

Where, DoubleStream is a sequence of primitive 
double-valued elements and the function 
returns a parallel 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