Open In App
Related Articles

LongStream forEachOrdered() method in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

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 :




// 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);
    }
}


Output:

2
3
4
5

Example 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);
    }
}


Output:

5
6
7
8
9
10

Example 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);
    }
}


Output :

5
6
7
8
9
10

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials