How to Map IntSteam to String object in Java

Java 8Object Oriented ProgrammingProgramming

To Map IntStream to String object, use mapToObj and within that set the values −

.mapToObj(val -> "z" + val)

Before that, we used range() on IntStream −

IntStream.range(1, 10)

The following is an example to map IntStream to String object in Java −

Example

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) throws Exception {
      IntStream.range(1, 10)
      .mapToObj(val -> "z" + val)
      .forEach(System.out::println);
   }
}

Output

z1
z2
z3
z4
z5
z6
z7
z8
z9
raja
Published on 09-May-2019 08:58:45
Advertisements