Friday, May 8, 2026
HomeLanguagesJavaIntStream forEachOrdered() method in Java

IntStream forEachOrdered() method in Java

IntStream forEachOrdered(IntConsumer action) performs an action for each element of this stream in encounter order. IntStream forEachOrdered(IntConsumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.

Syntax :

void forEachOrdered(IntConsumer action)

Parameter : IntConsumer represents an operation that accepts a single int-valued argument and returns no result. This is the primitive type specialization of Consumer for int.

Note : forEachOrdered(IntConsumer 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 IntStream forEachOrdered
// (IntConsumer action) in Java 8
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.of(2, 3, 4, 5);
  
        // Using IntStream.forEachOrdered
        stream.forEachOrdered(System.out::println);
    }
}


Output:

2
3
4
5

Example 2 :




// Java code for IntStream forEachOrdered
// (IntConsumer action) in Java 8
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.range(5, 11);
  
        // Using IntStream.forEachOrdered() on
        // sequential stream
        stream.forEachOrdered(System.out::println);
    }
}


Output:

5
6
7
8
9
10

Example 3 :




// Java code for IntStream forEachOrdered
// (IntConsumer action) in Java 8
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.range(5, 11);
  
        // Using IntStream.forEachOrdered() on
        // parallel stream
        stream.parallel().forEachOrdered(System.out::println);
    }
}


Output :

5
6
7
8
9
10
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12106 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6962 POSTS0 COMMENTS