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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements