Saturday, September 6, 2025
HomeLanguagesJavaDoubleStream mapToObj() in Java

DoubleStream mapToObj() in Java

DoubleStream mapToObj() returns an object-valued Stream consisting of the results of applying the given function.

Syntax:

<U> Stream<U> 
mapToObj(DoubleFunction<? 
extends U> mapper)

Parameters: This method accepts following parameters:

  1. U: The element type of the new stream.
  2. Stream : A sequence of elements supporting sequential and parallel aggregate operations.
  3. DoubleFunction : Represents a function that accepts an double-valued argument and produces a result.
  4. mapper : A stateless function to apply to each element.

Return Value: The function returns an object-valued Stream consisting of the results of applying the given function.

Below examples illustrate the mapToObj() method:

Example 1 :




// Java code for DoubleStream mapToObj
// (DoubleFunction mapper)
  
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a DoubleStream 
        DoubleStream stream = DoubleStream.of(3.4, 4.5, 
                                              6.7, 8.9);
  
        // Using DoubleStream mapToObj(DoubleFunction mapper)
        // and displaying an object-valued Stream 
        // consisting of the results of 
        // applying the given function
        stream.mapToObj(num ->{return num * num * num ;})
                           .forEach(System.out::println);
  
    }
}


Output:

39.303999999999995
91.125
300.76300000000003
704.969

Example 2 :




// Java code for DoubleStream mapToObj
// (DoubleFunction mapper)
  
import java.util.*;
import java.math.BigDecimal;
import java.util.stream.Stream;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a DoubleStream 
        DoubleStream stream = DoubleStream.of(3.4, 4.5, 
                                              6.7, 8.9);
          
        // Creating a Stream 
        // Using DoubleStream mapToObj(DoubleFunction mapper)
        Stream<BigDecimal> stream1 = stream
            .mapToObj(BigDecimal::valueOf);
                      
        // Displaying an object-valued Stream 
        // consisting of the results of 
        // applying the given function.
        stream1.forEach(num -> System.out.println
                    (num.add(BigDecimal.TEN)));
    }
}


Output:

13.4
14.5
16.7
18.9

Related Articles :

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS