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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 09-May-2019 08:54:41
Advertisements