Map to get substring and convert to int in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our stream:

Stream.of("u2", "h9", "s8", "l3")

Now, map to get the substring:

.map(s -> s.substring(1))

Convert to int and find the minimum:

.mapToInt(Integer::parseInt)
.min()

The following is an example to Map and get substring and convert to int:

Example

import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) throws Exception {
   Stream.of("u2", "h9", "s8", "l3")
      .map(s -> s.substring(1))
      .mapToInt(Integer::parseInt)
      .min()
      .ifPresent(System.out::println);
   }
}

Output

2
raja
Published on 02-May-2019 10:09:00
Advertisements