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