Here you can find the source of mergeData(List lstMain, List lstData, List lstKey)
Parameter | Description |
---|---|
lstMain | Main list |
lstData | List of data. Every item is also a list. |
lstKey | List of key |
public static void mergeData(List lstMain, List lstData, List lstKey)
//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; } } } } } }