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.
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.
You could do it like this:
mapC = mapA.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> mapB.get(e.getValue())));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With