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


Java LongStream parallel()用法及代碼示例


LongStream parallel()是java.util.stream.LongStream中的方法。此方法返回並行的LongStream,即,它可能返回自身,這是因為流已經存在,或者是因為基礎流狀態被修改為並行。

LongStream parallel()是中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。

用法:


LongStream parallel()

Where, LongStream is a sequence of 
primitive long-valued elements and the function 
returns a parallel LongStream.

下麵給出一些示例,以更好地理解該函數。
示例1:

// Java program to demonstrate working of 
// LongStream parallel() on a given range 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating a stream of longs 
        LongStream stream = LongStream.range(5L, 12L); 
  
        System.out.println("The corresponding "
                           + "parallel LongStream is :"); 
        stream.parallel().forEach(System.out::println); 
    } 
}

輸出:

The corresponding parallel LongStream is :
9
8
11
10
6
5
7

示例2:

// Printing sequential stream for the 
// same input as above example 1. 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        LongStream stream = LongStream.range(5L, 12L); 
  
        System.out.println("The corresponding "
                           + "sequential LongStream is :"); 
        stream.sequential().forEach(System.out::println); 
    } 
}

輸出:

The corresponding sequential LongStream is :
5
6
7
8
9
10
11

示例3:

// Java program to show sorted output 
// of parallel stream. 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating a stream of longs 
        LongStream stream = LongStream.of(3L, 4L, 1L, 5L, 
                                          2L, 3L, 9L); 
  
        System.out.println("The sorted parallel"
                           + " LongStream is :"); 
        stream.parallel().sorted().forEach(System.out::println); 
    } 
}

輸出:

The sorted parallel LongStream is :
4
2
3
9
3
5
1


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream parallel() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。