Get Substring and Convert to Int in Java



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

609 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements