Java List Remove remove(List list, T remove)

Here you can find the source of remove(List list, T remove)

Description

Removes an item from a list and returns the collections instance of this item, or null if not found or the collection is empty/null.

License

Apache License

Parameter

Parameter Description
T a parameter
list a parameter
remove a parameter

Declaration

public static <T> T remove(List<T> list, T remove) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.Iterator;

import java.util.List;
import java.util.Map;

public class Main {
    /**//from   w  w w. jav a2s  .  com
     * Removes an item from a list and returns the collections instance of this
     * item, or null if not found or the collection is empty/null.
     * 
     * @param <T>
     * @param list
     * @param remove
     * @return
     */
    public static <T> T remove(List<T> list, T remove) {
        if (isEmpty(list))
            return null;
        if (!list.contains(remove))
            return null;

        T item = get(list, remove);
        list.remove(item);
        return item;
    }

    public static boolean isEmpty(Iterable<?> i) {
        if (i instanceof Collection)
            return ((Collection<?>) i).isEmpty();
        return i == null || !i.iterator().hasNext();
    }

    public static boolean isEmpty(Map<?, ?> p_oCol) {
        return p_oCol == null || p_oCol.isEmpty();
    }

    /**
     * This method provides a way to obtain an object from a collection, given
     * an equal object (logical equals). In the collection and set interfaces
     * (even list) the assumption is that if you have an equal object why would
     * you need the one out of the collection. Because logically equal objects
     * can be physically different objects we provide this method to return out
     * of a collection the actual object in the collection ( for partial object
     * checking and untrusted object arguements that reference an existing item
     * in a collection ).
     * 
     * @param p_oCol
     * @param p_oObj
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, T p_oObj) {
        if (p_oObj == null)
            return null;
        if (isEmpty(p_oCol))
            return null;
        if (p_oCol instanceof List)
            return get(p_oCol, ((List<T>) p_oCol).indexOf(p_oObj));
        T oObj = null;
        T oTempObj = null;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (p_oObj.equals(oTempObj)) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    /**
     * Mostly provided for sets, Please keep in mind sets don't guarantee any
     * specific order. And it could be that for any one instance of a set this
     * method may return 2 different objects for the same index (dependent on
     * the set implementation) This method is useful for getting a single item
     * out of a collection when you don't know what type of collection you are
     * dealing with.
     * 
     * @param p_oCol
     * @param p_iIndex
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, int p_iIndex) {
        if (isEmpty(p_oCol))
            return null;
        if (p_iIndex < 0 || p_iIndex >= p_oCol.size())
            return null;
        if (p_oCol instanceof List)
            return ((List<T>) p_oCol).get(p_iIndex);

        T oObj = null;
        T oTempObj = null;
        int i = 0;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (i++ == p_iIndex) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    public static int size(Collection<?> col) {
        return col == null ? 0 : col.size();
    }
}

Related

  1. remove(List list, E object)
  2. remove(List list, int index)
  3. remove(List list, int index)
  4. remove(List list, int index, T defaultValue)
  5. remove(List list, int position)
  6. remove(List list, T value)
  7. remove(Object o, List oldList)
  8. removeAfter(List children, int index)
  9. removeAll(List list, Object[] elements)