Java Stream How to - Map String list to uppercase and then sort








Question

We would like to know how to map String list to uppercase and then sort.

Answer

import java.util.ArrayList;
import java.util.List;
//  www .j  av  a 2  s . c  om
public class Main {

  public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");


    stringCollection
    .stream()
    .map(String::toUpperCase)
    .sorted((a, b) -> b.compareTo(a))
    .forEach(System.out::println);
  }

}

The code above generates the following result.