Here you can find the source of joinRecords(List first, List second, String key)
Parameter | Description |
---|---|
first | first collection |
second | second collection |
key | specified key |
public static List joinRecords(List first, List second, String key)
//package com.java2s; /*//w ww. 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.Iterator; public class Main { /** * check each element of first list, and compare it to each element of second list. * element of list is <code>java.util.Map</code>, get value from map by specified key, if value * of element of first list are equals second's, invoke <code>java.util.Map#putAll</code> * of element of first list * * @param first first collection * @param second second collection * @param key specified key * @return first list */ public static List joinRecords(List first, List second, String key) { if (first != null && second != null) { for (int i = 0; i < first.size(); i++) { Map record1 = (Map) first.get(i); for (Iterator iterator1 = second.iterator(); iterator1.hasNext();) { Map record2 = (Map) iterator1.next(); if (record2.get(key) != null && record2.get(key).equals(record1.get(key))) { record1.putAll(record2); break; } } first.set(i, record1); } } return first; } }