LongStream forEachOrdered() method in Java
Last Updated :
06 Dec, 2018
LongStream forEachOrdered(LongConsumer action) performs an action for each element of this stream in encounter order. LongStream forEachOrdered(LongConsumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Syntax :
void forEachOrdered(LongConsumer action)
Parameter : LongConsumer represents an operation that accepts a single long-valued argument and returns no result. This is the primitive type specialization of Consumer for long.
Note : forEachOrdered(LongConsumer action) performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
Example 1 :
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.of(2L, 3L, 4L, 5L);
stream.forEachOrdered(System.out::println);
}
}
|
Example 2 :
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.range(5L, 11L);
stream.forEachOrdered(System.out::println);
}
}
|
Example 3 :
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.range( 5 , 11 );
stream.parallel().forEachOrdered(System.out::println);
}
}
|
Output :
5
6
7
8
9
10
Share your thoughts in the comments
Please Login to comment...