Java Collection Remove removeAll(Collection collection, Collection remove)

Here you can find the source of removeAll(Collection collection, Collection remove)

Description

Removes the elements in remove from collection.

License

Apache License

Parameter

Parameter Description
collection the collection from which items are removed (in the returned collection)
remove the items to be removed from the returned <code>collection</code>

Exception

Parameter Description
NullPointerException if either parameter is null

Return

a List containing all the elements of c except any elements that also occur in remove.

Declaration

public static List removeAll(Collection collection, Collection remove) 

Method Source Code


//package com.java2s;
/*//from   w w w  .j a v  a 2s  . co  m
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.ArrayList;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Removes the elements in <code>remove</code> from <code>collection</code>. That is, this
     * method returns a list containing all the elements in <code>c</code>
     * that are not in <code>remove</code>. The cardinality of an element <code>e</code>
     * in the returned collection is the same as the cardinality of <code>e</code>
     * in <code>collection</code> unless <code>remove</code> contains <code>e</code>, in which
     * case the cardinality is zero. This method is useful if you do not wish to modify
     * <code>collection</code> and thus cannot call <code>collection.removeAll(remove);</code>.
     * 
     * @param collection  the collection from which items are removed (in the returned collection)
     * @param remove  the items to be removed from the returned <code>collection</code>
     * @return a <code>List</code> containing all the elements of <code>c</code> except
     * any elements that also occur in <code>remove</code>.
     * @throws NullPointerException if either parameter is null
     * @since Commons Collections 3.2
     */
    public static List removeAll(Collection collection, Collection remove) {
        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;
    }
}

Related

  1. remove(Collection col, T value)
  2. remove(Collection collection, final int count)
  3. remove(Collection collection, T... items)
  4. remove(final Collection c, final Object elem)
  5. remove(final int index, final Collection collection)
  6. removeAll(Collection source, Set remove)
  7. removeAll(Collection c, Iterable elts)
  8. removeAll(Collection c, Object e)
  9. removeAll(Collection c, T... array)