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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。