Syntax error How to filter String stream and map to lower case in Java? Perform sort as well.

How to filter String stream and map to lower case in Java? Perform sort as well.



Let’s say the following is String array, which we have converted to List −

Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")

Now filter String stream and map to lower case −

.stream()
.filter(a-> a.startsWith("V"))
.map(String::toLowerCase)

To sort now, use the sorted() and display using forEach().

The following is an example to filter string stream and map to lowercase −

Example

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) throws Exception {
      Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")
      .stream()
      .filter(a-> a.startsWith("V"))
      .map(String::toLowerCase)
      .sorted()
      .forEach(System.out::println);
   }
}

Output

Vw
Updated on: 2019-07-30T22:30:26+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements