Java List Join joinRecords(List first, List second, String key)

Here you can find the source of joinRecords(List first, List second, String key)

Description

check each element of first list, and compare it to each element of second list.

License

Open Source License

Parameter

Parameter Description
first first collection
second second collection
key specified key

Return

first list

Declaration

public static List joinRecords(List first, List second, String key) 

Method Source Code


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

Related

  1. joinLists(List A, List B, double epsilon)
  2. joinLists(List... list)
  3. joinListToString(List lsStr, String sSeparator)
  4. joinLong(List lst, String prefix)
  5. joinNiceString(List strings)
  6. joinRelativePath(List components)
  7. joinSegments(List segments, int startIndex, int endIndex)
  8. joinString(java.util.List values, String delimiter)
  9. joinString(List array, String symbol)