IntStream parallel()是java.util.stream.IntStream中的方法。此方法返回一個並行的IntStream,即,它可能會返回自身,這是因為該流已經存在,或者是因為基礎流狀態已被修改為並行。
IntStream parallel()是中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:
IntStream parallel() Where, IntStream is a sequence of primitive int-valued elements and the function returns a parallel IntStream.
下麵給出一些示例,以更好地理解該函數。
示例1:
// Java program to demonstrate working of
// IntStream parallel() on a given range
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a stream of integers
IntStream stream = IntStream.range(5, 12);
System.out.println("The corresponding " +
"parallel IntStream is :");
stream.parallel().forEach(System.out::println);
}
}
輸出:
The corresponding parallel IntStream 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.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
IntStream stream = IntStream.range(5, 12);
System.out.println("The corresponding " +
"sequential IntStream is :");
stream.sequential().forEach(System.out::println);
}
}
輸出:
The corresponding sequential IntStream 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.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a stream of integers
IntStream stream = IntStream.of(3, 4, 1, 5, 2, 3, 9);
System.out.println("The sorted parallel" +
" IntStream is :");
stream.parallel().sorted().forEach(System.out::println);
}
}
輸出:
The sorted parallel IntStream is : 4 2 3 1 3 5 9
請注意,它仍然顯示為未排序。那是因為正在使用forEach()。要按排序順序處理項目,請使用forEachOrdered()。但是請注意,這否定了使用並行的優勢。
相關用法
- Java LongStream parallel()用法及代碼示例
- Java DoubleStream parallel()用法及代碼示例
- Java IntStream of()用法及代碼示例
- Java IntStream sum()用法及代碼示例
- Java IntStream sorted()用法及代碼示例
- Java IntStream sequential()用法及代碼示例
- Java IntStream concat()用法及代碼示例
- Java IntStream skip()用法及代碼示例
- Java IntStream summaryStatistics()用法及代碼示例
- Java IntStream range()用法及代碼示例
- Java IntStream mapToDouble()用法及代碼示例
- Java IntStream asDoubleStream()用法及代碼示例
- Java IntStream findFirst()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 IntStream parallel() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。