Here you can find the source of merge(Map
public static Map<String, ? extends Object> merge(Map<String, ? extends Object> src, Map<String, ? extends Object> dest)
//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; } }