IntStream forEachOrdered() method in Java

Java 8ProgrammingObject Oriented Programming

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
raja
Published on 26-Mar-2019 12:33:16
Advertisements