如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:44:37  来源:igfitidea点击:

How to convert Map to List in Java 8

javajava-8java-stream

提问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 Pairelements 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 mapoperation:

这是通过以下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 Pairclass have getSecondgetter.

我假设你的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())