Tuesday, November 18, 2025
HomeLanguagesJavaDoubleStream forEachOrdered() method in Java

DoubleStream forEachOrdered() method in Java

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

Syntax :

void forEachOrdered(DoubleConsumer action)

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

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


Output:

2.2
3.3
4.4
5.5

Example 2 :




// Java code for DoubleStream forEachOrdered
// (DoubleConsumer action) in Java 8
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an DoubleStream
        DoubleStream stream = 
              DoubleStream.of(5.3, 6.4, 7.3, 6.1);
  
        // Using DoubleStream.forEachOrdered() on
        // parallel stream
        stream.parallel().forEachOrdered(System.out::println);
    }
}


Output:

5.3
6.4
7.3
6.1
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6772 POSTS0 COMMENTS
Nicole Veronica
11921 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11992 POSTS0 COMMENTS
Shaida Kate Naidoo
6899 POSTS0 COMMENTS
Ted Musemwa
7155 POSTS0 COMMENTS
Thapelo Manthata
6852 POSTS0 COMMENTS
Umr Jansen
6844 POSTS0 COMMENTS