Here you can find the source of mergeFamilies(Map
public static <K, V extends Number> void mergeFamilies(Map<K, V> familySource, Map<K, V> familyTarget)
//package com.java2s; //License from project: Open Source License import java.util.Map; public class Main { public static <K, V extends Number> void mergeFamilies(Map<K, V> familySource, Map<K, V> familyTarget) { // methods iterates thru source family and copies its entries to target family // in case key already exists in both families - then the values are added if (familySource == null) { return; }/*from w ww . ja v a 2 s .c o m*/ for (K key : familySource.keySet()) { if (!familyTarget.containsKey(key)) { familyTarget.put(key, familySource.get(key)); } else { if (familyTarget.get(key).getClass() == Integer.class) { Integer sum = familyTarget.get(key).intValue() + familySource.get(key).intValue(); familyTarget.put(key, (V) sum); } else { Double sum = familyTarget.get(key).doubleValue() + familySource.get(key).doubleValue(); familyTarget.put(key, (V) sum); } } } } }