Java Map Merge merge(List> list)

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

Description

Merge/combine a list of Map into a single map

License

Apache License

Parameter

Parameter Description
list a List<Map<Character, Long>>

Return

merge of all Maps

Declaration

public static Map<Character, Long> merge(List<Map<Character, Long>> list) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Map;
import java.util.HashMap;
import java.util.List;

public class Main {
    /**/*from w w  w. j  ava2s.  c  om*/
     * Merge/combine a list of Map<Character, Long> into a single map
     * 
     * @param list a List<Map<Character, Long>>
     * @return merge of all Maps
     */
    public static Map<Character, Long> merge(List<Map<Character, Long>> list) {
        //
        if ((list == null) || (list.isEmpty())) {
            return null;
        }
        //
        Map<Character, Long> merged = new HashMap<>();
        //
        for (Map<Character, Long> map : list) {
            //
            if ((map == null) || (map.isEmpty())) {
                continue;
            }
            //
            for (Map.Entry<Character, Long> entry : map.entrySet()) {
                char base = entry.getKey();
                Long count = merged.get(base);
                if (count == null) {
                    merged.put(base, entry.getValue());
                } else {
                    merged.put(base, (count + entry.getValue()));
                }
            }
        }
        //
        return merged;
    }
}

Related

  1. deepMerge(Map original, Map newMap)
  2. merge(final Map lhs, final Map rhs)
  3. merge(final Map map1, final Map map2)
  4. merge(final Map... maps)
  5. merge(final Map> targetContext, final Map> newContext)
  6. merge(Map map1, Map map2)
  7. merge(Map options, Map addition)
  8. merge(Map into, Map with)
  9. merge(Map left, Map right)