【发布时间】:2021-11-11 18:32:38
【问题描述】:
我在尝试对哈希映射进行排序并将其收集到列表时遇到了这个答案:
Sort a Map<Key, Value> by values
我试过了:
return myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList(Map.Entry::getKey, Map.Entry::getValue, (k,v) -> k, LinkedList::new));
但是,我收到此错误:
Cannot resolve constructor 'LinkedList'
我要做的就是在按值对 HashMap 进行排序后将我的键收集到一个列表中。我做错了什么?
【问题讨论】:
-
Collectors.toList()不接受任何参数。可能您会看到其他错误。 Alex 和 Efimov 的答案是正确的,只需使用toList()
标签: java collections java-stream