Java List Remove removeAll(List toRemoveFrom, Collection elementsToRemove)

Here you can find the source of removeAll(List toRemoveFrom, Collection elementsToRemove)

Description

Convenient method to remove elements from a collection.

License

Apache License

Parameter

Parameter Description
toRemoveFrom collection to remove elements from
elementsToRemove elements to remove

Return

new collection without the removed elements

Declaration

public static <T> List<T> removeAll(List<T> toRemoveFrom, Collection<T> elementsToRemove) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//w w  w .  j a va  2s  . c om
     * Convenient method to remove elements from a collection.
     * @param toRemoveFrom collection to remove elements from
     * @param elementsToRemove elements to remove
     * @return new collection without the removed elements
     */
    public static <T> List<T> removeAll(List<T> toRemoveFrom, Collection<T> elementsToRemove) {
        ArrayList<T> subtract = new ArrayList<>(toRemoveFrom);
        subtract.removeAll(elementsToRemove);
        return subtract;
    }

    /**
     * Convenient method to remove elements from a collection.
     * @param toRemoveFrom collection to remove elements from
     * @param elementsToRemove elements to remove
     * @return new collection without the removed elements
     */
    public static <T> Set<T> removeAll(Set<T> toRemoveFrom, Collection<T> elementsToRemove) {
        Set<T> subtract = new HashSet<>(toRemoveFrom);
        subtract.removeAll(elementsToRemove);
        return subtract;
    }
}

Related

  1. removeAll(List list, Object[] elements)
  2. removeAll(List list, List objects)
  3. removeAll(List idxs, List values)
  4. removeAll(List strings, String string)
  5. removeAll(List list, List indexes)
  6. removeAllNull(List list)
  7. removeAllNulls(List list)
  8. removeBlankLine(List lines)
  9. removeByRef(List list, Object obj)