How to convert Map to List in Java 8

Yakiv Holovko :

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.

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a list of objects into a map in Java 8?

how to convert list to map by java8?

Java 8: How to convert List<String> to Map<String,List<String>>?

How to convert List to Map with indexes using stream - Java 8?

Java 8 Convert List of List of String to Map

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

Java: How to convert List to Map

How to convert a Map to List in Java?

Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

Convert List to map using java 8

convert list to map using java8

Java 8: Convert 2 Lists into List of Map

Convert list to map using Java8

How to Convert a Map<String, List<String>> to Map<String, String> in java 8

Using Java 8 streams how do I convert Map<String, List<Something>> to Map<String, Integer>?

Java8: How to Convert Map<X, List<Y>> to Map<Y,X> using Stream?

How to convert List<AnyClassObject> to List<Map<String, String>> using Stream in java 8?

Java 8 Stream API: how to convert a List to a Map<Long, Set> having repeated keys in the List?

How do I convert a List<Employee> to Map<Employee, List<String>> using java8?

How to convert a stream to a HashMap (not Map) in Java 8

Java8 convert List of Map to List of string

Convert a List into a Map with value as a List of Objects in Java 8

Convert list of Object to Map<Key, List<Object>> with java 8 stream

Convert Map<List> to List<NewType> with new java 8 streams

Convert List<Map<Long, String>> to List<Long> Java 8

Java8 convert list<P> in to Map<Integer, List<P>>

Convert Map<String,List<Person>> to Map<String,List<Employee>>, using Java 8 streams. I did this but how do this without for loop

How to convert List of Map into Map of List using Java Streams

Java: how to convert a List<?> to a Map<String,?>