Java List Merge merge(List original, List merger)

Here you can find the source of merge(List original, List merger)

Description

merge

License

Open Source License

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
    private static void merge(List original, List merger) 

Method Source Code

//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));
        }
    }
}

Related

  1. merge(final List> listOfLists)
  2. merge(final List list, final String seperator)
  3. merge(final List a, final List b)
  4. merge(final List aList, final List bList)
  5. merge(final String[] input, final String[] list)
  6. merge(List bs)
  7. merge(List left, List right)
  8. merge(List list1, List list2)
  9. merge(List> list)