Java Map Merge merge(Map src, Map dest)

Here you can find the source of merge(Map src, Map dest)

Description

merge

License

Open Source License

Declaration

public static Map<String, ? extends Object> merge(Map<String, ? extends Object> src,
        Map<String, ? extends Object> dest) 

Method Source Code


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

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

public class Main {

    public static Map<String, ? extends Object> merge(Map<String, ? extends Object> src,
            Map<String, ? extends Object> dest) {
        if (src == null || src.size() == 0) {
            return dest;
        }// ww w.  j  av a 2s  . c o  m
        if (dest == null || dest.size() == 0) {
            return src;
        }

        Map<String, Object> result = new HashMap<>();
        src.forEach((key, value) -> {
            Object valueDesc = dest.get(key);
            if (valueDesc != null) {
                result.put(key, mergeToList(value, valueDesc));
            } else {
                result.put(key, value);
            }
        });
        dest.forEach((key, value) -> {
            if (result.get(key) == null) {
                result.put(key, value);
            }
        });
        return result;

    }

    public static List mergeToList(Object src, Object... args) {
        List valList = new ArrayList();
        add(valList, src);
        for (Object arg : args) {
            add(valList, arg);
        }
        return valList;
    }

    public static List add(List valList, Object src) {
        if (src == null) {
            return valList;
        }
        if (src instanceof List) {
            valList.addAll((List) src);
        } else {
            valList.add(src);
        }
        return valList;
    }
}

Related

  1. merge(Map into, Map with)
  2. merge(Map left, Map right)
  3. merge(Map map, K key, V value)
  4. merge(Map map, Map... maps)
  5. merge(Map master, Map copy)
  6. merge(Map first, Map second)
  7. merge(Map map1, Map map2)
  8. merge(Map mapPriorityLeast, Map mapPriorityFirst)
  9. mergeAggCompStatsTopoPageSpout(Map accSpoutStats, Map spoutStats)