Transfer each element in a Collection to a new value via a function - Java java.util

Java examples for java.util:Collection Creation

Description

Transfer each element in a Collection to a new value via a function

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;

public class Main {
  public static <T, R> Collection<T> common(Collection<T> collection1, Collection<R> collection2,
      Function<T, R> transformer) {
    Collection<T> common = new ArrayList<T>();
    for (T t : collection1) {
      R tansformed = transformer.apply(t);
      if (collection2.contains(tansformed))
        common.add(t);//from   w ww. ja va  2 s .  c om
    }

    return common;
  }
}

Related Tutorials