4

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.

8
  • 5
    Have you tried anything? Read the documentation? What concrete problem did you face? Regardless of the syntax, what would be your strategy to achieve that? Commented Oct 25, 2018 at 20:39
  • 2
    map.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> mapB.get(e.getValue()))) Commented Oct 25, 2018 at 20:41
  • @BillK I think your solution will neither compile, nor it is what the OP asked Commented Oct 25, 2018 at 23:33
  • @BillK dont think so your solution will work. I am trying to create a 3rd map from values of A matching keys of B Commented Oct 26, 2018 at 0:25
  • @BillK I completely agree with what you said regarding streams. But your code still won't compile. (1) addAll is in Collection interface. Map has putAll instead. (2) You are trying to add all Map<A, B> to Map<A,List<C>>, which won't work! Commented Oct 26, 2018 at 0:26

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You could do it like this:

mapC = mapA.entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey, e -> mapB.get(e.getValue())));

6 Comments

Why create an entry set, then make a stream out of it and then finally collect them, if you cam just use Map.forEach which directly gives you the key value pair. It makes it easier and simpler to understand.
@uneq95 Stream operations don't work in the sequence as you might think they do. Performance, memory consumption and readability are similar to those of your solution, if not better.
Yes, I know that it doesn't work in sequence. Neither do we require the sequence to be maintained here, as we just want to insert in a hashmap, which too doesn't maintain the sequence. I was arguing on the creation of entry set in the first place.
@uneq95 I was talking about the sequence of stream operations, like conversion to stream, running filter, map, etc. and then collecting. I was not talking about the ordering of elements in the result. Try asking a new question to see which solution is better and you'll get the answer that they are similar.
@uneq95 There's no "creation" of an entry set. It's just a thin view to the underlying map.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.