Here you can find the source of mergeValue(Map
static void mergeValue(Map<String, Object> obj, String key, Object value)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; import java.util.Map; public class Main { static void mergeValue(Map<String, Object> obj, String key, Object value) { if (obj == null) { return; }// w ww . j a v a2 s . c om List<Object> values = (List<Object>) obj.get(key); if (values == null) { values = new ArrayList<Object>(); obj.put(key, values); } if ("@list".equals(key) || (value instanceof Map && ((Map<String, Object>) value).containsKey("@list")) || !deepContains(values, value)) { values.add(value); } } public static boolean deepContains(List<Object> values, Object value) { for (final Object item : values) { if (deepCompare(item, value, false)) { return true; } } return false; } public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) { if (v1 == null) { return v2 == null; } else if (v2 == null) { return v1 == null; } else if (v1 instanceof Map && v2 instanceof Map) { final Map<String, Object> m1 = (Map<String, Object>) v1; final Map<String, Object> m2 = (Map<String, Object>) v2; if (m1.size() != m2.size()) { return false; } for (final String key : m1.keySet()) { if (!m2.containsKey(key) || !deepCompare(m1.get(key), m2.get(key), listOrderMatters)) { return false; } } return true; } else if (v1 instanceof List && v2 instanceof List) { final List<Object> l1 = (List<Object>) v1; final List<Object> l2 = (List<Object>) v2; if (l1.size() != l2.size()) { return false; } // used to mark members of l2 that we have already matched to avoid // matching the same item twice for lists that have duplicates final boolean alreadyMatched[] = new boolean[l2.size()]; for (int i = 0; i < l1.size(); i++) { final Object o1 = l1.get(i); Boolean gotmatch = false; if (listOrderMatters) { gotmatch = deepCompare(o1, l2.get(i), listOrderMatters); } else { for (int j = 0; j < l2.size(); j++) { if (!alreadyMatched[j] && deepCompare(o1, l2.get(j), listOrderMatters)) { alreadyMatched[j] = true; gotmatch = true; break; } } } if (!gotmatch) { return false; } } return true; } else { return v1.equals(v2); } } public static Boolean deepCompare(Object v1, Object v2) { return deepCompare(v1, v2, false); } }