Here you can find the source of merge(List original, List merger)
@SuppressWarnings({ "rawtypes", "unchecked" }) private static void merge(List original, List merger)
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.Map; import java.util.Set; public class Main { @SuppressWarnings("rawtypes") private static void merge(Object original, Object merger) { if (original instanceof List && merger instanceof List) { merge((List) original, (List) merger); } else if (original instanceof Map && merger instanceof Map) { merge((Map) original, (Map) merger); } else if (merger == null) { // no-op } else {/*from www .ja v a2 s . c om*/ throw new RuntimeException("Unrecognized combination"); } } @SuppressWarnings({ "unchecked", "rawtypes" }) private static void merge(Map original, Map merger) { Set originalKS = original.keySet(); Set mergerKS = merger.keySet(); // {tres=[{cuatro=1}]} => {tres=[{cinco=2}]} for (Object key : originalKS) { if (mergerKS.contains(key)) { merge(original.get(key), merger.get(key)); } } for (Object key : mergerKS) { if (!originalKS.contains(key)) { original.put(key, merger.get(key)); } } } @SuppressWarnings({ "rawtypes", "unchecked" }) private static void merge(List original, List merger) { int i; for (i = 0; i < original.size() && i < merger.size(); i++) { merge(original.get(i), merger.get(i)); } if (merger.size() > original.size()) { for (; i < merger.size(); i++) original.add(merger.get(i)); } } }