マップ値がコレクションされた値の大きさによって地図エントリをソートするには?

Janly:

値がコレクションである値によってマップエントリをソートする方法はありますか?私は彼らのコレクションサイズでエントリをソートしたいです。これまでの私のコード:

public Map<Object, Collection<Object>> sortByCollectionSize() {
    return myMap.entrySet().stream().sorted(Map.Entry.<Object, Collection<Object>>comparingByValue())
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
また:

あなたはそれが作業得ることができますComparator.comparingIntように。

return myMap.entrySet()
            .stream()
            .sorted(Comparator.comparingInt(e -> e.getValue().size()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=205870&siteId=1
おすすめ