Java Map Merge mergeFamilies(Map familySource, Map familyTarget)

Here you can find the source of mergeFamilies(Map familySource, Map familyTarget)

Description

merge Families

License

Open Source License

Declaration

public static <K, V extends Number> void mergeFamilies(Map<K, V> familySource, Map<K, V> familyTarget) 

Method Source Code

//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);
                }
            }
        }
    }
}

Related

  1. mergeCustomProperties(Map copyTo, Map copyFrom)
  2. mergeDefaults(Map content, Map defaults)
  3. mergeEntries( Map dest, Iterable> entries)
  4. mergeEnv(Map newEnv, Map env)
  5. mergeEnvironmentVariables(Map environmentSource, Map environmentDestination, String environmentVariable)
  6. mergeFixedValueConditions(Map> left, Map> right)
  7. mergeIfAbsent(Map map, Map toMerge)
  8. mergeImportedPermissionsWithExistingPermissions( Map> existingRoleIdsToActionIds, Map importedRoleIdsToActionIds)
  9. mergeListWithMap(List from, Map to)