Example usage for java.util Collection removeAll

List of usage examples for java.util Collection removeAll

Introduction

In this page you can find the example usage for java.util Collection removeAll.

Prototype

boolean removeAll(Collection<?> c);

Source Link

Document

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

Usage

From source file:Main.java

public static <T> Collection<T> not(Collection<T> all, Collection<T> not) {
    Collection<T> remain = new LinkedList<T>(all);
    remain.removeAll(not);
    return remain;
}

From source file:Main.java

/**
 * Returns all items in collection1 that are not in collection2
 *///from www. j  av a 2s  . c o  m
public static <T> Collection<T> subtractCollections(Collection<? extends T> collection1,
        Collection<? extends T> collection2) {
    Collection<T> result = new ArrayList<T>(collection1);
    result.removeAll(collection2);
    return result;
}

From source file:Main.java

/**
 * This method compare generic <code>Collection</code> and remove from first collection
 * elements which included in second <code>Collection</code>.
 *
 * @param originalCollection original <code>Collection</code>
 * @param compareCollection  collection from which the elements will be compared
 * @param <T>                collection type
 * @return collection Collection of elements from the first collection,
 *                    which are included in the second collection
 * @see Collection/*from  w  w  w  .  ja  va  2 s . c  o  m*/
 * @see T
 */
public static <T> Collection<T> firstCollectionMinusSecondCollection(Collection<T> originalCollection,
        Collection<T> compareCollection) {
    Collection<T> resultCollection = new ArrayList<>(originalCollection);
    resultCollection.removeAll(compareCollection);

    return resultCollection;
}

From source file:Main.java

public static void filterNull(Collection<?> collection) {
    if (isNotEmpty(collection)) {
        collection.removeAll(Collections.singleton(null));
    }//from  ww  w.  j a v a 2s  .com
}

From source file:Main.java

public static <E> Collection<E> minus(Collection<E> lhs, Collection<E> rhs) {
    Collection<E> collection = new ArrayList<>(lhs);
    lhs.removeAll(rhs);
    return collection;
}

From source file:Main.java

public static <E> boolean removeAllIfNotNull(final Collection<? super E> collection,
        final Collection<? extends E> c) {
    return (collection != null) && (c != null) && collection.removeAll(c);
}

From source file:Main.java

public static <E> Collection<E> diffLeft(Collection<E> l, Collection<E> r) {
    if (isEmpty(l) || isEmpty(r))
        return l;

    List<E> s = new ArrayList<E>(l);
    s.removeAll(r);/*  w  w  w. j  ava2 s  . c o m*/
    r.removeAll(l);

    return s;
}

From source file:org.mule.util.ArrayUtils.java

public static Object[] setDifference(Object[] a, Object[] b) {
    Collection aCollecn = new HashSet(Arrays.asList(a));
    Collection bCollecn = Arrays.asList(b);
    aCollecn.removeAll(bCollecn);
    return aCollecn.toArray();
}

From source file:Main.java

/**
 * Given two collections of elements of type <T>, return a collection with the items which only appear in one
 * collection or the other//from  w  ww.j  ava2  s .  c  o  m
 * 
 * @param <T>
 *            Type
 * @param collection1
 *            Collection #1
 * @param collection2
 *            Collection #2
 * @return Collection with items unique to each list
 */
public static <T> Collection<T> getNonOverlapping(Collection<T> collection1, Collection<T> collection2) {
    Collection<T> result = union(collection1, collection2);

    if (!isEmpty(result)) {
        result.removeAll(intersect(collection1, collection2));
        if (result instanceof ArrayList) {
            ((ArrayList<T>) result).trimToSize();
        }
    }

    return result;
}

From source file:Main.java

public static <T> Collection<T> setDiff(Collection<T> itemsToStartWith, Collection<T> itemsToSubtract) {
    Collection<T> retval = new ArrayList<T>();
    if (itemsToStartWith == null)
        return new ArrayList<T>();
    retval.addAll(itemsToStartWith); // make copy so you don't mutate original
    if (itemsToSubtract != null)
        retval.removeAll(itemsToSubtract);
    return retval;
}