Join 2 maps Java 8 using streams

ayush sanghvi :

I have 2 Maps

Map<A, B> mapA
Map<B, List<C>> mapB

I want to join these maps on the values in mapA & keys in mapB the result should be

Map<A,List<C>> mapC

I am willing to know how can I do it using streams in Java8.

A,B,C for simplicty, all of these are strings in my case.

uneq95 :

You can iterate over the map and easily construct the new map.

Map<A,List<C>> mapC = new HashMap<>();

mapA.forEach((key,value)->mapC.put(key, mapB.get(value)));

You can use this link, which compares the efficiency of different ways to iterate over the key-value pairs, to select which method you want to use.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=74493&siteId=1