ı am trying to merge more than one hashmaps also sum the values of same key, ı want to explain my problem with toy example as follows
HashMap
This method should do it (in Java 5+)
public static Map mergeAndAdd(Map... maps) {
Map result = new HashMap<>();
for (Map map : maps) {
for (Map.Entry entry : map.entrySet()) {
K key = entry.getKey();
Integer current = result.get(key);
result.put(key, current == null ? entry.getValue() : entry.getValue() + current);
}
}
return result;
}