Java Stream How to - Filter String stream and map to upper case then sort








Question

We would like to know how to filter String stream and map to upper case then sort.

Answer

/* ww  w . j  av  a 2s.c o  m*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws Exception {
    Arrays.asList("a1", "a2", "b1", "c2", "c1")
    .stream()
    .filter(s -> s.startsWith("c"))
    .map(String::toUpperCase)
    .sorted()
    .forEach(System.out::println);
  }

}

The code above generates the following result.