当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DoubleStream parallel()用法及代码示例


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

DoubleStream parallel()是中间操作。这些操作总是很懒。在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


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 DoubleStream parallel() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。