Given a string:
String str = "Charlie Jennifer Charlie Bob Charlie Charlie Bob Jennifer Alice Alice";
Need to count name occurrences in the given string and output sorted result by occurrence count (desc), then by name. Here is my solution:
Arrays.stream(str.split(" "))
.collect(groupingBy(identity(), counting())).entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue()
.reversed()
.thenComparing(Map.Entry.comparingByKey()))
.forEach(System.out::println);
My question is: do you know more elegant solution using Java Stream API ?