Here you can find the source of removeAll(Collection> collection, Iterator> itemsToRemove)
Parameter | Description |
---|---|
collection | the collection from which items are to be removed |
itemsToRemove | the items to remove |
public static boolean removeAll(Collection<?> collection, Iterator<?> itemsToRemove)
//package com.java2s; import java.util.Collection; import java.util.Iterator; import java.util.Set; public class Main { /**/* w w w . ja v a 2s. c o m*/ * Removes from a collection all items retrieved from a specified iterator. This can be used to * implement {@link Collection#removeAll(Collection)} for a collection that has a fast (e.g. * logarithmic or constant time) {@linkplain Collection#remove(Object) remove} operation. For * collections with slower (e.g. linear) remove operations, {@link #filter(Collection, Iterator, boolean)} * will be the better choice. * * <p>Examples:<pre> * // Using CollectionUtils.removeAll() * {@literal @}Override public boolean removeAll(Collection<?> c) { * return CollectionUtils.removeAll(this, c.iterator()); * } * * // Alternate implementation instead using CollectionUtils.filter() * {@literal @}Override public boolean removeAll(Collection<?> c) { * return CollectionUtils.filter(c, iterator(), true); * } * </pre> * * @param collection the collection from which items are to be removed * @param itemsToRemove the items to remove * @return true if the collection was modified (e.g. one or more items actually removed) */ public static boolean removeAll(Collection<?> collection, Iterator<?> itemsToRemove) { boolean modified = false; while (itemsToRemove.hasNext()) { Object o = itemsToRemove.next(); // in case collection allows duplicates, we need to repeat remove operation until it // returns false to make sure we get all of them while (true) { if (collection.remove(o)) { modified = true; } else { // all removed, move on to next break; } } } return modified; } /** * Removes from a set all items retrieved from a specified iterator. * * <p>The only difference between this method and {@link #removeAll(Collection, Iterator)} is * that this version assumes the collection cannot have duplicates (after all, it is a set). So * it invokes {@link Set#remove(Object)} only once per item to remove. Since other collections * may have duplicates, the other version must invoke the method until it returns false * (indicating that no more occurrences are in the collection). * * @param collection the collection from which items are to be removed * @param itemsToRemove the items to remove * @return true if the collection was modified (e.g. one or more items actually removed) * * @see #removeAll(Collection, Iterator) */ public static boolean removeAll(Set<?> collection, Iterator<?> itemsToRemove) { boolean modified = false; while (itemsToRemove.hasNext()) { if (collection.remove(itemsToRemove.next())) { modified = true; } } return modified; } }