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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 09-May-2019 08:59:50
Advertisements