3

I have two Maps, both sharing the same keys, .

Map<Long/*JOIN.ID*/, Long/*Temp ID*/> tempIDsMap;
Map<Long/*JOIN.ID*/, Long/*Real ID*/> realIDsMap;

What I want to get (maybe using Java 8 Stream API and avoiding loops) is the JOIN of these maps on the JOIN.ID keys, to get a new Map like below:

Map<Long/*Temp ID*/. Long/*Real ID*/> realIDsByTempMap;
1Z10
  • 2,801
  • 7
  • 33
  • 82
  • I don't know of any given framework to do that for you, so you have to create your own logic to complete this task. What have you tried so far? Can you provide the code, you have tried? Can you point out specific issues or problems? – Korashen Aug 08 '18 at 08:28
  • Why not using a loop though? – Lino Aug 08 '18 at 08:33
  • what do you mean by join by key? your maps are `Map`. So which value is to select in final map(as value can be different for same key)? Do you want to create `Map>` ? – Shubhendu Pramanik Aug 08 '18 at 08:38

1 Answers1

4

Use Collectors.toMap:

Map<Long,Long> realIDsByTempMap = 
    tempIDsMap.entrySet()
              .stream()
              .collect(Collectors.toMap(Map.Entry::getValue,
                       e -> realIDsMap.get(e.getKey())));
Eran
  • 387,369
  • 54
  • 702
  • 768