如何按集合大小总和对 Map(String, List(Set(Long))) 进行排序? 我有一个像这样的 HashMap:
myMap.put("monday", [[3215, 5654], [5345], [3246, 7686, 4565]]) // 6 Long elements in total
myMap.put("tuesday", [[3215, 5654], [5345, 2879, 6734], [3246, 7686, 4565]]) // 8 Long elements in total
myMap.put("wednesday", [[9845, 2521], [0954]]) // 3 Long elements in total
我希望 myMap 像这样排序:
("tuesday", [[3215, 5654], [5345, 2879, 6734], [3246, 7686, 4565]]) // 8 Long elements in total
("monday", [[3215, 5654], [5345], [3246, 7686, 4565]]) // 6 Long elements in total
("wednesday", [[9845, 2521], [0954]]) // 3 Long elements in total
最佳答案
使用LinkedHashMap
进行排序:
Map<String, List<Set<Long>>> result = map.entrySet()
.stream()
.sorted(Comparator.comparingInt(e->e.getValue().stream().mapToInt(Set::size).sum()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
关于java - 如何按集合大小总和对 Map<String, List<Set<Long>>> 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198992/