How can I update a Java HashMap with a new Map? I have a requirement where I have to iteratively update a HashMap with a subsequent new map I get.
Update has two parts
- Checks if the latest map has any new key not present in the final map and puts them
- Updates the keys which are already present in the final map with the latest map values.
Currently, I am doing this
currentDataMap.forEach(finalMap::putIfAbsent);
finalMap.replaceAll(currentDataMap::getOrDefault);
Is there better way or one step way?