两个映射的空安全合并。
//package com.nowjava; import java.util.LinkedHashMap; import java.util.Map;//来自 时 代 J a v a - nowjava.com public class Main { /** * Null-safe merge of two maps. If both parameters are null it returns empty map. * If one of the maps is null, it returns the other one. If a key is present in two maps, * the value in the merged map will be taken from the overriding map. * @param overridingMap The map overriding values in the base map * @param baseMap The base map * @return merged map */ public static Map<Object, Object> mergeMaps( Map<Object, Object> overridingMap, Map<Object, Object> baseMap) { Map<Object, Object> mergedMap = new LinkedHashMap<>(); if (baseMap == null && overridingMap != null) { mergedMap.putAll(overridingMap); } else if (overridingMap == null && baseMap != null) { mergedMap.putAll(baseMap);// 来自 n o w j a v a . c o m - 时 代