Java List Combine combineList(List l1, List l2)

Here you can find the source of combineList(List l1, List l2)

Description

Combine two lists (l1 -> l2) and return a new list, compare the item using List.contains(), so implement equals()

License

Open Source License

Parameter

Parameter Description
l1 a parameter
l2 a parameter

Return

Combined list

Declaration

public static List combineList(List l1, List l2) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**/* ww  w . j a v  a2  s  .  c o  m*/
     * Combine two lists (l1 -> l2) and return a new list, compare the item using List.contains(), so implement equals()
     * @param l1
     * @param l2
     * @return Combined list
     */
    public static List combineList(List l1, List l2) {
        List resultList = new ArrayList();
        if (l1 != null && l2 != null) {
            resultList.addAll(l1);
            for (int i = 0; i < l2.size(); i++) {
                Object o = l2.get(i);
                if (!resultList.contains(o)) {
                    resultList.add(o);
                }
            }
        } else if (l1 != null && l2 == null) {
            resultList.addAll(l1);
        } else if (l1 == null && l2 != null) {
            resultList.addAll(l2);
        }
        return resultList;
    }
}

Related

  1. combineArray(List... p)
  2. combineAsLists(Object one, Object two)
  3. combineAux(List> collections, List objectAccumulator, List> resultList)
  4. combineFloat(List nums)
  5. combineLines(List lines)
  6. combineLists(List keys, List values)
  7. combineLists(List a, List b)
  8. combineString(List lstInput, String strToken)
  9. combineStringList(List sList)