Copies all elements that match a predicate. - Java java.util

Java examples for java.util:Collection Element Remove

Description

Copies all elements that match a predicate.

Demo Code

import java.util.Collection;
import java.util.function.Predicate;

public class Main {
  /**//from ww  w . jav  a2s. c om
   * Copies all elements that match a predicate.
   * 
   * Every element of the first collection for which the predicate holds (i.e.
   * returns true) is added to the second collection.
   */
  public static <T> void copySelective(Collection<? extends T> source, Collection<? super T> target,
      Predicate<T> filter) {
    for (T t : source) {
      if (filter.test(t)) {
        target.add(t);
      }
    }
  }
}

Related Tutorials