Removes all elements from the Collection - Java java.util

Java examples for java.util:Collection Element Remove

Description

Removes all elements from the Collection

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w w  w  . j a v a2 s  .c o m*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/
//package com.java2s;

import java.util.Collection;

public class Main {
    /**
     * Removes all elements from the {@link Collection}. 
     * @param <T> The type of the {@link Collection}s elements.
     * @param collection The {@link Collection} to remove from.
     * @param elementsToRemove The elements to remove.
     * @return {@code true} if the {@link Collection} changed as result of this call.
     */
    public static <T> boolean removeAll(Collection<T> collection,
            T... elementsToRemove) {
        if (collection != null && elementsToRemove != null) {
            boolean result = false;
            for (T toAdd : elementsToRemove) {
                result = collection.remove(toAdd) || result;
            }
            return result;
        } else {
            return false;
        }
    }
}

Related Tutorials