Java Program to Map String list to lowercase and sort

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our String List −

List list = new ArrayList<>();
list.add("ABC");
list.add("CDE");
list.add("GHI");
list.add("MNO");
list.add("GWE");
list.add("WDF");
list.add("JYH");
list.add("TYU");

Map String List to lowercase −

list
.stream()
.map(String::toLowerCase)

After that, perform sorting −

sorted((val1, val2) -> val2.compareTo(val1))

The following is an example to Map string list to lowercase and sort −

Example

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(final String[] args) {
      List list = new ArrayList<>();
      list.add("ABC");
      list.add("CDE");
      list.add("GHI");
      list.add("MNO");
      list.add("GWE");
      list.add("WDF");
      list.add("JYH");
      list.add("TYU");
      list
      .stream()
      .map(String::toLowerCase)
      .sorted((val1, val2) -> val2.compareTo(val1))
      .forEach(System.out::println);
   }
}

Output

wdf
tyu
mno
jyh
gwe
ghi
cde
abc
raja
Published on 09-May-2019 09:01:22
Advertisements