/** * Removes from the given iterable all the elements that evaluate to true * * @param iterable * @param predicate * @param <T> * @param <I> * @return the given iterable */ @NonNull public static <T, I extends Iterable<T>> I removeAll( @NonNull I iterable, @NonNull Evaluable<? super T> predicate) { for (Iterator<T> iter = iterable.iterator(); iter.hasNext(); ) if (predicate.eval(iter.next())) iter.remove(); return iterable; }
/** * Removes all elements from the given iterable that evalute to true, and adds them to the given * collection * * @param iterable * @param collection * @param predicate * @param <T> * @param <C> * @return the given collection */ @NonNull public static <T, C extends Collection<T>> C move( @NonNull Iterable<T> iterable, @NonNull C collection, @NonNull Evaluable<T> predicate) { for (Iterator<T> iter = iterable.iterator(); iter.hasNext(); ) { T element = iter.next(); if (predicate.eval(element)) { iter.remove(); collection.add(element); } } return collection; }