Java Collection Remove minus(Collection primaryCollection, Collection toBeRemovedCollection, Collection target)

Here you can find the source of minus(Collection primaryCollection, Collection toBeRemovedCollection, Collection target)

Description

Return a new Collection containing the elements of Collection a minus elements of Collection b

License

Open Source License

Parameter

Parameter Description
T a parameter
primaryCollection a parameter
toBeRemovedCollection a parameter
target a collection that will be returned by this method. I will be cleared by this method before inserting the required elements.

Return

a new Collection containing the elements of Collection a minus elements of Collection b

Declaration

public static <T> Collection<T> minus(Collection<T> primaryCollection, Collection<T> toBeRemovedCollection,
        Collection<T> target) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**//from   ww  w. j  a v a2 s . com
     * Return a new Collection containing the elements of Collection a minus elements of Collection b 
     * @param <T>
     * @param primaryCollection
     * @param toBeRemovedCollection
     * @param target a collection that will be returned by this method. I will be cleared by this method before inserting the required elements. 
     * @return a new Collection containing the elements of Collection a minus elements of Collection b
     */
    public static <T> Collection<T> minus(Collection<T> primaryCollection, Collection<T> toBeRemovedCollection,
            Collection<T> target) {
        target.clear();

        for (T elem : primaryCollection) {
            if (!toBeRemovedCollection.contains(elem)) {
                target.add(elem);
            }
        }
        return target;
    }
}

Related

  1. getItemsStartingWith(Collection items, String prefix, boolean removePrefix)
  2. getRemovedItems(Collection oldValues, Collection newValues)
  3. remove(Collection c, Object o)
  4. remove(Collection collection, Object object)
  5. remove(Collection p_collection, int p_index, int p_numberOfObjects)
  6. remove(Collection col, T value)