Java List Merge mergeData(List lstMain, List lstData, List lstKey)

Here you can find the source of mergeData(List lstMain, List lstData, List lstKey)

Description

Merge the list of data to main list by list of key

License

Open Source License

Parameter

Parameter Description
lstMain Main list
lstData List of data. Every item is also a list.
lstKey List of key

Declaration

public static void mergeData(List lstMain, List lstData, List lstKey) 

Method Source Code


//package com.java2s;
/*//w  w  w  .j a  v  a  2s .c om
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class Main {
    /**
     * Merge the list of data to main list by list of key
     *
     * @param lstMain Main list
     * @param lstData List of data. Every item is also a list.
     * @param lstKey  List of key
     */
    public static void mergeData(List lstMain, List lstData, List lstKey) {
        if (lstMain == null || lstData == null || lstData.size() == 0 || lstKey == null || lstKey.size() == 0) {
            return;
        }
        for (int index1 = 0, n = lstMain.size(); index1 < n; index1++) {
            Map mapMain = (Map) lstMain.get(index1);
            Map keyValue = new HashMap();
            for (int index2 = 0, m = lstKey.size(); index2 < m; index2++) {
                Object key = lstKey.get(index2);
                keyValue.put(key, mapMain.get(key));
            }
            for (int index2 = 0, m = lstData.size(); index2 < m; index2++) {
                List lstChild = (List) lstData.get(index2);
                for (int index3 = 0, s = lstChild.size(); index3 < s; index3++) {
                    Map mapChild = (Map) lstChild.get(index3);
                    boolean flag = true;
                    for (int index4 = 0, t = lstKey.size(); index4 < t; index4++) {
                        Object key = lstKey.get(index4);
                        Object valueFromMain = keyValue.get(key);
                        Object valueFromChild = mapChild.get(key);
                        if ((valueFromMain != null && !valueFromMain.equals(valueFromChild))
                                || (valueFromMain == null && valueFromChild != null)) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        mapMain.putAll(mapChild);
                        break;
                    }
                }
            }
        }
    }
}

Related

  1. merge2ListsWithoutDup(List list1, List list2)
  2. mergeBandY(List A, int z, int y, int yn)
  3. mergeBytes(List packages)
  4. mergeCollectionsToList(Collection... cols)
  5. mergeCoords(List x, List y)
  6. mergedView(final List left, final List right)
  7. mergeErrorMsgList(List errorMsgs)
  8. mergeException(List t)
  9. mergeExcludedFieldLists(String[] superClassExcludes, String[] localClassExcludes)