, : Map.Entry.comparingByValue. , . , :
Map.Entry.comparingByValue(Comparator.reverseOrder())
.
Collections.sort(list, Map.Entry.comparingByValue(Comparator.reverseOrder()));
.
Map , Stream. , Stream.of(Arrays.asList("...").split(" ")) Pattern.compile(" ").splitAsStream("..."), .
Map<String, Long> bag =
Pattern.compile(" ")
.splitAsStream("one o'clock two o'clock three o'clock rock")
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Map<String, Long> sortedBag =
bag.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(v1, v2) -> { throw new IllegalStateException(); },
LinkedHashMap::new
));
, LinkedHashMap, .
:
{o'clock=3, rock=1, one=1, three=1, two=1}
StreamEx, :
Map<String, Long> bag =
StreamEx.split("one o'clock two o'clock three o'clock rock", " ")
.sorted()
.runLengths()
.reverseSorted(Map.Entry.comparingByValue())
.toCustomMap(LinkedHashMap::new);
, runLengths(). Stream<String, Long>, , . , Stream ["foo", "foo", "bar"] Stream [Entry("foo", 2), Entry("bar", 1)]. , LinkedHashMap.
, 2 .