當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。