IntStream forEachOrdered() method in Java


The forEachOrdered() method in Java assures that each element is processed in order for streams that have a defined encounter order.

The syntax is as follows

void forEachOrdered(IntConsumer action)

Here, the action parameter is a non-interfering action to be performed on the elements.

Create an IntStream and add elements to the stream

IntStream intStream = IntStream.of(50, 70, 80, 100, 130, 150, 200);

Now, use the forEachOrdered() method to display the stream elements in order

intStream.forEachOrdered(System.out::println);

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

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(50, 70, 80, 100, 130, 150, 200);
      intStream.forEachOrdered(System.out::println);
   }
}

Output

50
70
80
100
130
150
200

Updated on: 30-Jul-2019

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements