Syntax error How to Map IntSteam to String object in Java

How to Map IntSteam to String object in Java



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
Updated on: 2019-07-30T22:30:26+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements