Java Utililty Methods List Combine

List of utility methods to do List Combine

Description

The list of methods to do List Combine are organized into topic(s).

Method

ListcombineList(List l1, List l2)
Combine two lists (l1 -> l2) and return a new list, compare the item using List.contains(), so implement equals()
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;
MapcombineLists(List keys, List values)
Combines two lists into map, using first list as keys and second as values
if (keys.size() != values.size()) {
    throw new IllegalArgumentException("Cannot combine lists with dissimilar sizes");
if (keys.isEmpty() && values.isEmpty()) {
    return Collections.emptyMap();
int size = values.size();
Map<K, V> map = new LinkedHashMap<>(size);
...
ListcombineLists(List a, List b)
Combine lists.
ArrayList<E> result = new ArrayList<E>();
result.addAll(a);
result.addAll(b);
return result;
StringcombineString(List lstInput, String strToken)
Combine string array as string, split by token string
if (lstInput == null) {
    return null;
StringBuffer sb = new StringBuffer();
for (Iterator iterator = lstInput.iterator(); iterator.hasNext();) {
    sb.append(iterator.next().toString());
    if (iterator.hasNext()) {
        sb.append(strToken);
...
StringcombineStringList(List sList)
combine String List
return combineList(STRING_SPLIT_TOKEN, sList);
ListcombineSubtractArrays(List a1, List a2)
Changes first array to have same elements as second Given arrays a1 and a2 Returns array a1 with elements present in a2, but without ones that aren't in a2
Iterator<T> it = a1.iterator();
while (it.hasNext()) {
    T e1 = it.next();
    if (!a2.contains(e1)) {
        a1.remove(e1);
if (a1.size() == a2.size()) {
...
StringgetCombinedColumnName(final List columnNames, final boolean sortByName)
get Combined Column Name
final int size = columnNames.size();
if (size == 0) {
    throw new IllegalArgumentException();
if (size == 1) {
    return columnNames.get(0);
if (sortByName) {
...
MapputCombinedList(Map map, Object key, Object value)
put Combined List
if (key == null)
    throw new IllegalArgumentException("putCombinedList passed null key");
Object existingValue = map.get(key);
Object combinedList = combineAsLists(existingValue, value);
if (combinedList != null)
    map.put(key, combinedList);
return map;
List>recursiveCombine(List>> toCombinate, int index)
recursive Combine
if (index == toCombinate.size() - 1) {
    return new ArrayList<List<T>>(toCombinate.get(index));
} else {
    List<List<T>> next = recursiveCombine(toCombinate, index + 1);
    List<List<T>> result = new ArrayList<List<T>>();
    List<List<T>> currentContainer = toCombinate.get(index); 
    for (List<T> current : currentContainer) {
        for (List<T> n : next) {
...