Java中的 LongStream forEachOrdered() 方法
LongStream forEachOrdered(LongConsumer action)按遇到顺序为此流的每个元素执行一个操作。 LongStream forEachOrdered(LongConsumer action) 是一个终端操作,即它可以遍历流以产生结果或副作用。
句法 :
void forEachOrdered(LongConsumer action)
参数: LongConsumer 表示接受单个长值参数且不返回结果的操作。这是消费者长期以来的原始类型特化。
注意: 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