How to convert Map to List in Java 8

♀尐吖头ヾ 提交于 2019-12-03 11:40:11

问题


How to convert a Map<String, Double> to List<Pair<String, Double>> in 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.


回答1:


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>>.

This is done with the map operation:

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>.

Finally, you want to collect that into a List, so we can use the built-in toList() collector.

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()).




回答2:


Note that if you want efficient implementation, 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.

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.




回答3:


    public List<TeamResult> process(final Map<String, Team> aggregatedMap) {
   return aggregatedMap.entrySet()
                       .stream()
                       .map(e -> new TeamResult(e.getKey(),e.getValue()))
                       .collect(Collectors.toList());
    }



回答4:


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

    List<String> list = map.keySet().stream()
                .sorted((k1, k2)->map.get(k2)- map.get(k1))
                .limit(2)
                .collect(Collectors.toList())


来源:https://stackoverflow.com/questions/35107550/how-to-convert-map-to-list-in-java-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!