Here you can find the source of mergeDefaults(Map
Parameter | Description |
---|---|
content | the content |
defaults | the defaults |
@SuppressWarnings({ "unchecked" }) public static void mergeDefaults(Map<String, Object> content, Map<String, Object> defaults)
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; public class Main { /**//from w w w.j a v a 2 s . co m * Merge defaults. * * @param content the content * @param defaults the defaults */ @SuppressWarnings({ "unchecked" }) public static void mergeDefaults(Map<String, Object> content, Map<String, Object> defaults) { for (Map.Entry<String, Object> defaultEntry : defaults.entrySet()) { if (!content.containsKey(defaultEntry.getKey())) { content.put(defaultEntry.getKey(), defaultEntry.getValue()); } else { if (content.get(defaultEntry.getKey()) instanceof Map && defaultEntry.getValue() instanceof Map) { mergeDefaults( (Map<String, Object>) content.get(defaultEntry .getKey()), (Map<String, Object>) defaultEntry.getValue()); } else if (content.get(defaultEntry.getKey()) instanceof List && defaultEntry.getValue() instanceof List) { List<Object> mergedList = new ArrayList<Object>(); mergedList.addAll((Collection<Object>) defaultEntry .getValue()); mergedList.addAll((Collection<Object>) content .get(defaultEntry.getKey())); content.put(defaultEntry.getKey(), mergedList); } } } } }