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

Here you can find the source of addAllUniqueId(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> addAllUniqueId(List<T> aList, List<T> theObjects) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**/*from   w ww  .j  a  v  a 2  s.c  o  m*/
     * Adds all object from second list to first list (creates first list if missing).
     */
    public static <T> List<T> addAllUniqueId(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 (!containsId(aList, 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> addAllUniqueId(List<T> aList, T... theObjects) {
        // If list is null, create it
        if (aList == null)
            aList = new ArrayList();

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

        // Return list
        return aList;
    }

    /**
     * Returns whether list contains identical given object (accepts null list).
     */
    public static boolean containsId(List aList, Object anObj) {
        return indexOfId(aList, anObj) >= 0;
    }

    /**
     * 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 index of identical given object in given list.
     */
    public static int indexOfId(List aList, Object anObj) {
        // Iterate over list objects and return index if identical exists
        for (int i = 0, iMax = size(aList); i < iMax; i++)
            if (anObj == aList.get(i))
                return i;

        // Return -1 if identical doesn't exist
        return -1;
    }

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

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }
}

Related

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