We would like to know how to sort String Stream by length.
/* w ww . ja v a 2 s .c om*/ import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<String> friends = Arrays.asList("CSS", "HTML", "Oracle", "Dart"); Comparator<String> compByLength = (aName, bName) -> aName.length() - bName.length(); friends.stream().sorted(compByLength).forEach(System.out::println); } }
The code above generates the following result.