如何根据 Collection 值的大小对 Map 进行排序?
How can I sort a Map based upon on the size of its Collection values?
我有一个 HashMap
这样的:
Map<String, List<String>> map = new HashMap<>();
map.put("USA", Arrays.asList("CA","IA","IL"));
map.put("India", Arrays.asList("MUM","CAL"));
map.put("Canada", Arrays.asList("TOR"));
我想根据列表值的大小对地图进行升序排序。我该怎么做?
在这种情况下,我想订购加拿大、印度、美国的钥匙。
你有两个问题。
地图不支持排序。
SortedMap 不支持按值排序,仅支持按键排序。
因此,使用 Map 或 SortedMap 对您没有帮助。您需要做的是遍历映射并将每个 Entry<String, ArrayList<String>>
放入一个集合(例如列表)中,然后使用自定义比较对列表进行排序。看这个例子 TreeMap sort by value or this example Sorting LinkedHashMap
HashMap
没有保证的迭代顺序,因此您需要收集到 LinkedHashMap
才能使排序有意义。
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toMap;
Map<String, List<String>> sorted = map.entrySet().stream()
.sorted(comparingInt(e -> e.getValue().size()))
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(a, b) -> { throw new AssertionError(); },
LinkedHashMap::new
));
AssertionError
被抛出是因为组合器函数是 ,我们没有使用它。
如果你觉得可读性更好,也可以使用comparingByValue
:
import static java.util.Map.Entry.comparingByValue;
Map<String, List<String>> sorted = map.entrySet().stream()
.sorted(comparingByValue(comparingInt(List::size)))
// ... as above
您可以简单地将散列映射到 Pair() 的列表,然后对列表进行排序
val map: MutableMap<String, List<String>> = HashMap()
map["USA"] = listOf("CA", "IA", "IL")
map["India"] = listOf("MUM", "CAL")
map["Canada"] = listOf("TOR")
val sortedPairs = map.map { Pair(it.key, it.value) }.sortedBy { it.second.size }.toMap()
println("sorted List: $sortedPairs")
public static <K,V extends Collection> Map<K,V> sortMap(Map<K,V> map){
return map.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
我有一个 HashMap
这样的:
Map<String, List<String>> map = new HashMap<>();
map.put("USA", Arrays.asList("CA","IA","IL"));
map.put("India", Arrays.asList("MUM","CAL"));
map.put("Canada", Arrays.asList("TOR"));
我想根据列表值的大小对地图进行升序排序。我该怎么做?
在这种情况下,我想订购加拿大、印度、美国的钥匙。
你有两个问题。
地图不支持排序。
SortedMap 不支持按值排序,仅支持按键排序。
因此,使用 Map 或 SortedMap 对您没有帮助。您需要做的是遍历映射并将每个 Entry<String, ArrayList<String>>
放入一个集合(例如列表)中,然后使用自定义比较对列表进行排序。看这个例子 TreeMap sort by value or this example Sorting LinkedHashMap
HashMap
没有保证的迭代顺序,因此您需要收集到 LinkedHashMap
才能使排序有意义。
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toMap;
Map<String, List<String>> sorted = map.entrySet().stream()
.sorted(comparingInt(e -> e.getValue().size()))
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(a, b) -> { throw new AssertionError(); },
LinkedHashMap::new
));
AssertionError
被抛出是因为组合器函数是
如果你觉得可读性更好,也可以使用comparingByValue
:
import static java.util.Map.Entry.comparingByValue;
Map<String, List<String>> sorted = map.entrySet().stream()
.sorted(comparingByValue(comparingInt(List::size)))
// ... as above
您可以简单地将散列映射到 Pair() 的列表,然后对列表进行排序
val map: MutableMap<String, List<String>> = HashMap()
map["USA"] = listOf("CA", "IA", "IL")
map["India"] = listOf("MUM", "CAL")
map["Canada"] = listOf("TOR")
val sortedPairs = map.map { Pair(it.key, it.value) }.sortedBy { it.second.size }.toMap()
println("sorted List: $sortedPairs")
public static <K,V extends Collection> Map<K,V> sortMap(Map<K,V> map){
return map.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}