LongStream forEachOrdered(LongConsumer action)以遇到順序對此流的每個元素執行一個動作。 LongStream forEachOrdered(LongConsumer action)是一種終端操作,即,它可以遍曆流以產生結果或副作用。
用法:
void forEachOrdered(LongConsumer action)
參數:LongConsumer表示一個接受單個long值參數且不返回結果的操作。長期以來,這是Consumer的原始類型專業化。
注意:如果流具有已定義的遇到順序,則forEachOrdered(LongConsumer action)以該流的遇到順序對此流的每個元素執行一個動作。
示例1:
// Java code for LongStream forEachOrdered
// (LongConsumer action) in Java 8
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.of(2L, 3L, 4L, 5L);
// Using LongStream.forEachOrdered
stream.forEachOrdered(System.out::println);
}
}
輸出:
2 3 4 5
示例2:
// Java code for LongStream forEachOrdered
// (LongConsumer action) in Java 8
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.range(5L, 11L);
// Using LongStream.forEachOrdered() on
// sequential stream
stream.forEachOrdered(System.out::println);
}
}
輸出:
5 6 7 8 9 10
示例3:
// Java code for LongStream forEachOrdered
// (LongConsumer action) in Java 8
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.range(5, 11);
// Using LongStream.forEachOrdered() on
// parallel stream
stream.parallel().forEachOrdered(System.out::println);
}
}
輸出:
5 6 7 8 9 10
相關用法
- Java IntStream forEachOrdered()用法及代碼示例
- Java DoubleStream forEachOrdered()用法及代碼示例
- Java Stream forEachOrdered()用法及代碼示例
- Java LongStream forEach()用法及代碼示例
- Java LongStream generate()用法及代碼示例
- Java LongStream.Builder accept()用法及代碼示例
- Java LongStream sum()用法及代碼示例
- Java LongStream of()用法及代碼示例
- Java LongStream findFirst()用法及代碼示例
- Java LongStream builder()用法及代碼示例
- Java LongStream boxed()用法及代碼示例
- Java LongStream limit()用法及代碼示例
- Java LongStream mapToDouble()用法及代碼示例
- Java LongStream asDoubleStream()用法及代碼示例
- Java LongStream sorted()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream forEachOrdered() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。