Java List Value Add All addAllUnique(List aList, List theObjects)

Here you can find the source of addAllUnique(List aList, List theObjects)

Description

Adds all object from second list to first list (creates first list if missing).

License

Open Source License

Declaration

public static <T> List<T> addAllUnique(List<T> aList, List<T> theObjects) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//from  w  ww  .j  ava2  s .  co m
     * Adds all object from second list to first list (creates first list if missing).
     */
    public static <T> List<T> addAllUnique(List<T> aList, List<T> theObjects) {
        // If list is null, create it
        if (aList == null)
            aList = new ArrayList();

        // Add objects unique
        for (T object : theObjects)
            if (!aList.contains(object))
                aList.add(object);

        // Return list
        return aList;
    }

    /**
     * Adds all object from second list to first list (creates first list if missing).
     */
    public static <T> List<T> addAllUnique(List<T> aList, T... theObjects) {
        return addAllUnique(aList, aList != null ? aList.size() : 0, theObjects);
    }

    /**
     * Adds all object from second list to first list (creates first list if missing).
     */
    public static <T> List<T> addAllUnique(List<T> aList, int anIndex, T... theObjects) {
        // If list is null, create it
        if (aList == null)
            aList = new ArrayList();

        // Add objects unique
        for (int i = 0; i < theObjects.length; i++) {
            T object = theObjects[i];
            if (!aList.contains(object))
                aList.add(anIndex++, object);
        }

        // Return list
        return aList;
    }

    /**
     * Returns whether list contains given object (accepts null list).
     */
    public static boolean contains(List aList, Object anObj) {
        return aList != null && aList.contains(anObj);
    }

    /**
     * Adds an object to the given list and returns list (creates list if missing).
     */
    public static <T> List<T> add(List<T> aList, T anObj) {
        // If list is null, create list
        if (aList == null)
            aList = new Vector();

        // Add object
        aList.add(anObj);

        // Return list
        return aList;
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }
}

Related

  1. addAll(T[] array, List list)
  2. addAllTo(List procArgs, String[] args)
  3. addAllTo(List list, T... args)
  4. addAllToANewList(Collection... collections)
  5. addAllToList(List dest, Collection src)
  6. addAllUnique(List l1, T element)
  7. addAllUniqueId(List aList, List theObjects)
  8. addAllUniqueToList(List objects, List objectsToAdd)
  9. addAllWords(String text, List result)