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:
- U: The element type of the new stream.
- Stream : A sequence of elements supporting sequential and parallel aggregate operations.
- DoubleFunction : Represents a function that accepts an double-valued argument and produces a result.
- 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 :
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.DoubleStream;
class GFG {
public static void main(String[] args)
{
DoubleStream stream = DoubleStream.of( 3.4 , 4.5 ,
6.7 , 8.9 );
stream.mapToObj(num ->{ return num * num * num ;})
.forEach(System.out::println);
}
}
|
Output:
39.303999999999995
91.125
300.76300000000003
704.969
Example 2 :
import java.util.*;
import java.math.BigDecimal;
import java.util.stream.Stream;
import java.util.stream.DoubleStream;
class GFG {
public static void main(String[] args)
{
DoubleStream stream = DoubleStream.of( 3.4 , 4.5 ,
6.7 , 8.9 );
Stream<BigDecimal> stream1 = stream
.mapToObj(BigDecimal::valueOf);
stream1.forEach(num -> System.out.println
(num.add(BigDecimal.TEN)));
}
}
|
Output:
13.4
14.5
16.7
18.9
Related Articles :