Java Map Merge mergeMapList(List> list)

Here you can find the source of mergeMapList(List> list)

Description

merge Map List

License

Open Source License

Declaration

public static <K, V> Map<K, V> mergeMapList(List<Map<K, V>> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static <K, V> Map<K, V> mergeMapList(List<Map<K, V>> list) {
        Map<K, V> ret = new HashMap<K, V>();

        for (Map<K, V> listEntry : list) {
            if (listEntry == null) {
                continue;
            }//  w  w w . j  ava2  s . c  om
            for (Entry<K, V> mapEntry : listEntry.entrySet()) {
                K key = mapEntry.getKey();
                V value = mapEntry.getValue();

                V retValue = (V) add(ret.get(key), value);

                ret.put(key, retValue);
            }
        }

        return ret;
    }

    public static Object add(Object oldValue, Object newValue) {
        if (oldValue == null) {
            return newValue;
        }

        if (oldValue instanceof Long) {
            if (newValue == null) {
                return (Long) oldValue;
            } else {
                return (Long) oldValue + (Long) newValue;
            }
        } else if (oldValue instanceof Double) {
            if (newValue == null) {
                return (Double) oldValue;
            } else {
                return (Double) oldValue + (Double) newValue;
            }
        } else {
            return null;
        }
    }
}

Related

  1. mergeListWithMap(List from, Map to)
  2. mergeMap(Map map, Object[]... pairs)
  3. mergeMap(Map primaryValues, Map defaultValues)
  4. mergeMap(Map first, Map second)
  5. mergeMapIntoMap(Map> source, Map> destination)
  6. mergeMaps(Map m1, Map m2)
  7. mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)
  8. mergeMaps(Map dest, Map inserts)
  9. mergeMaps(Map into, Map from, boolean override)