LongStream forEachOrdered() method in Java


The forEachOrdered() method in Java performs an action for each element of this stream, guaranteeing that each element is processed in encounter order for streams that have a defined encounter order.

The syntax is as follows −

void forEachOrdered(LongConsumer action)

Here, parameter wrapper is a non-interfering action to perform on the elements. LongConsumer represents an operation that accepts a single long-valued argument and returns no result.

To use the LongStream class in Java, import the following package −

import java.util.stream.LongStream;

The following is an example to implement LongStream forEachOrdered() method in Java −

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;

public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(10000L, 7000L, 15000L);
      longStream.forEachOrdered(System.out::println);
   }
}

Output

10000
7000
15000

Updated on: 30-Jul-2019

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements