如何在 Java 8 中将 Map 转换为 List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35107550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Map to List in Java 8
提问by Yakiv Holovko
How to convert a Map<String, Double>
to List<Pair<String, Double>>
in Java 8?
如何将转换Map<String, Double>
到List<Pair<String, Double>>
使用Java 8?
I wrote this implementation, but it is not efficient
我写了这个实现,但效率不高
Map<String, Double> implicitDataSum = new ConcurrentHashMap<>();
//....
List<Pair<String, Double>> mostRelevantTitles = new ArrayList<>();
implicitDataSum.entrySet().stream().
.sorted(Comparator.comparing(e -> -e.getValue()))
.forEachOrdered(e -> mostRelevantTitles.add(new Pair<>(e.getKey(), e.getValue())));
return mostRelevantTitles;
I know that it should works using .collect(Collectors.someMethod())
. But I don't understand how to do that.
我知道它应该使用.collect(Collectors.someMethod())
. 但我不明白该怎么做。
回答by Tunaki
Well, you want to collect Pair
elements into a List
. That means that you need to map your Stream<Map.Entry<String, Double>>
into a Stream<Pair<String, Double>>
.
好吧,您想将Pair
元素收集到List
. 这意味着您需要将您的映射Stream<Map.Entry<String, Double>>
到Stream<Pair<String, Double>>
.
This is done with the map
operation:
这是通过以下map
操作完成的:
Returns a stream consisting of the results of applying the given function to the elements of this stream.
返回一个流,该流由将给定函数应用于此流的元素的结果组成。
In this case, the function will be a function converting a Map.Entry<String, Double>
into a Pair<String, Double>
.
在这种情况下,该函数将是一个将 a 转换Map.Entry<String, Double>
为 a的函数Pair<String, Double>
。
Finally, you want to collect that into a List
, so we can use the built-in toList()
collector.
最后,您想将其收集到一个 中List
,以便我们可以使用内置toList()
收集器。
List<Pair<String, Double>> mostRelevantTitles =
implicitDataSum.entrySet()
.stream()
.sorted(Comparator.comparing(e -> -e.getValue()))
.map(e -> new Pair<>(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Note that you could replace the comparator Comparator.comparing(e -> -e.getValue())
by Map.Entry.comparingByValue(Comparator.reverseOrder())
.
请注意,您可以取代比较Comparator.comparing(e -> -e.getValue())
的Map.Entry.comparingByValue(Comparator.reverseOrder())
。
回答by Tagir Valeev
Note that if you want efficientimplementation, you should consider this:
请注意,如果您想要高效的实施,您应该考虑:
List<Pair<String, Double>> mostRelevantTitles =
implicitDataSum.entrySet()
.stream()
.map(e -> new Pair<>(e.getKey(), e.getValue()))
.collect(Collectors.toList());
mostRelevantTitles.sort(Comparators.comparing(Pair::getSecond, Comparator.reverseOrder()));
I assume that your Pair
class have getSecond
getter.
我假设你的Pair
班级有getSecond
吸气剂。
Using the sorted()
stream pipeline step you create intermediate buffer, store everything to that buffer, convert it into array, sort that array, then store the result into the ArrayList
. My approach, though less functional, stores data directly into the target ArrayList
, then sorts it in-place without any additional copying. So my solution would take less time and intermediate memory.
使用sorted()
流管道步骤创建中间缓冲区,将所有内容存储到该缓冲区,将其转换为数组,对该数组进行排序,然后将结果存储到ArrayList
. 我的方法虽然功能较少,但将数据直接存储到 target 中ArrayList
,然后就地对其进行排序,无需任何额外的复制。所以我的解决方案将花费更少的时间和中间内存。
回答by ABHAY JOHRI
public List<TeamResult> process(final Map<String, Team> aggregatedMap) {
return aggregatedMap.entrySet()
.stream()
.map(e -> new TeamResult(e.getKey(),e.getValue()))
.collect(Collectors.toList());
}
回答by Gayathri Krishnan
Sort the Map based on values in reverse order and collect the keys in list and also limit only first 2 results in the list
以相反的顺序根据值对 Map 进行排序并收集列表中的键,并仅限制列表中的前 2 个结果
List<String> list = map.keySet().stream()
.sorted((k1, k2)->map.get(k2)- map.get(k1))
.limit(2)
.collect(Collectors.toList())