Java 8: how to 'JOIN' two Maps having the same Keys?

1Z10 :

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;
Eran :

Use Collectors.toMap:

Map<Long,Long> realIDsByTempMap = 
    tempIDsMap.entrySet()
              .stream()
              .collect(Collectors.toMap(Map.Entry::getValue,
                       e -> realIDsMap.get(e.getKey())));

Guess you like

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