How to sort a list of strings by length using Lambda expressions
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ShortByLengthOfString {
public static void main(String[] args) {
// How to sort a list of strings by length using Lambda expressions
String[] str = { "Rajesh Kumar", "Rajesh", "Kumar", "R4R" };
List<String> list = Stream.of(str).sorted((o1, o2) -> (o2.length() - o1.length())).collect(Collectors.toList());
System.out.print(list);
}
}
OutPut
[Rajesh Kumar, Rajesh, Kumar, R4R]