Java Utililty Methods Collection Remove

List of utility methods to do Collection Remove

Description

The list of methods to do Collection Remove are organized into topic(s).

Method

Tremove(final int index, final Collection collection)
remove
if (!isEmptyOrNull(collection)) {
    int i = 0;
    for (Iterator<T> it = collection.iterator(); it.hasNext();) {
        final T removed = it.next();
        if (i == index) {
            it.remove();
            return removed;
        i++;
return null;
ListremoveAll(Collection collection, Collection remove)
Removes the elements in remove from collection.
List list = new ArrayList();
for (Iterator iter = collection.iterator(); iter.hasNext();) {
    Object obj = iter.next();
    if (remove.contains(obj) == false) {
        list.add(obj);
return list;
...
voidremoveAll(Collection source, Set remove)
remove All
Iterator i = remove.iterator();
while (i.hasNext()) {
    source.remove(i.next());
booleanremoveAll(Collection c, Iterable elts)
Remove the given elements from a collection.
if (elts instanceof Collection<?>) {
    return c.removeAll((Collection<?>) elts);
} else {
    boolean result = false;
    for (Object elt : elts) {
        result |= c.remove(elt);
    return result;
...
voidremoveAll(Collection c, Object e)
Removes all occurrences of e from c.
if (c instanceof List) {
    List<?> list = (List<?>) c;
    int index = -1;
    do {
        index = list.lastIndexOf(e);
        if (index != -1)
            list.remove(index);
    } while (index != -1);
...
CollectionremoveAll(Collection c, T... array)
Removes objects in array to the given collection
for (T obj : array)
    c.remove(obj);
return c;
CollectionremoveAll(Collection c, T... array)
Removes objects in array to the given collection
for (T obj : array) {
    c.remove(obj);
return c;
voidremoveAll(Collection ret, Object[] elements)
remove All
if (elements != null)
    for (int i = 0; i < elements.length; i++)
        ret.remove(elements[i]);
ListremoveAll(Collection collection, Collection remove)
remove All
List<T> list = new ArrayList<T>();
for (Iterator<T> iter = collection.iterator(); iter.hasNext();) {
    T obj = iter.next();
    if (remove.contains(obj) == false) {
        list.add(obj);
return list;
...
booleanremoveAll(Collection collection, T... elementsToRemove)
Removes all elements from the Collection .
if (collection != null && elementsToRemove != null) {
    boolean result = false;
    for (T toAdd : elementsToRemove) {
        result = collection.remove(toAdd) || result;
    return result;
} else {
    return false;
...