Executes a function on each item in a Collection and returns the results in a new List - Java java.util

Java examples for java.util:Collection Search

Description

Executes a function on each item in a Collection and returns the results in a new List

Demo Code


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

public class Main {
  /**/*from  w w w  .j a va 2  s  . c om*/
   * Executes a function on each item in a {@link Collection} and returns the
   * results in a new {@link List}
   *
   * @param coll
   *          the collection to process
   * @param func
   *          the Function to execute
   * @return a list of the transformed objects
   */
  public static List transform(Collection coll, Function func) {
    List result = new ArrayList();
    for (Iterator i = coll.iterator(); i.hasNext();) {
      result.add(func.apply(i.next()));
    }
    return result;
  }
}

Related Tutorials