Java Collection Remove removeAll(final Collection collection, final Collection remove)

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

Description

Removes the elements in remove from collection.

License

Apache License

Parameter

Parameter Description
E the element type
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 <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) 

Method Source Code

//package com.java2s;
/*/* ww w . java  2s . c om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.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>collection</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>.
     * <p>
     * This implementation iterates over <code>collection</code>, checking each element in
     * turn to see if it's contained in <code>remove</code>. If it's not contained, it's added
     * to the returned list. As a consequence, it is advised to use a collection type for
     * <code>remove</code> that provides a fast (e.g. O(1)) implementation of
     * {@link Collection#contains(Object)}.
     *
     * @param <E>  the element type
     * @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 3.2
     */
    public static <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) {
        final List<E> list = new ArrayList<E>();
        for (final E obj : collection) {
            if (!remove.contains(obj)) {
                list.add(obj);
            }
        }
        return list;
    }
}

Related

  1. removeAll(Collection collection, Collection remove)
  2. removeAll(Collection collection, T... elementsToRemove)
  3. removeAll(Collection left, Collection right)
  4. removeAll(Collection source, Collection remove)
  5. removeAll(final Collection c, final Object... array)
  6. removeAll(final Collection collection, final T... objects)
  7. removeAllElements(Collection data, T value)
  8. removeAllFromCollection(Collection collection, Collection toRemove)
  9. removeAllFromSet(AbstractSet collection, Collection toRemove)