How to Map double to int Object with mapToInt and mapToObj in Java?


At first, we have set the stream −

Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)

Now to Map double to int Object, use mapToObj −

.mapToInt(Double::intValue)
.mapToObj(a -> "val" + a)

The following is an example to Map double to int Object with mapToInt and mapToObj −

Example

import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) throws Exception {
      Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)
      .mapToInt(Double::intValue)
      .mapToObj(a -> "val" + a)
      .forEach(System.out::println);
   }
}

Output

val3
val4
val7
val8
val9
val10
val12
val15

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements