/** * Transforms all elements from the inputIterator with the given transformer * and adds them to the outputCollection. * <p/> * If the input iterator or transformer is null, the result is an empty * list. * * @param inputIterator * the iterator to get the input from, may be null * @param transformer * the transformer to use, may be null * @return the transformed result (new list) */ public static <I, O> Collection<O> collect(Iterator<I> inputIterator, Transformer<? super I, ? extends O> transformer) { ArrayList<O> answer = new ArrayList<O>(); collect(inputIterator, transformer, answer); return answer; }
/** * Transforms all elements from inputCollection with the given transformer * and adds them to the outputCollection. * <p/> * If the input collection or transformer is null, there is no change to the * output collection. * * @param inputCollection * the collection to get the input from, may be null * @param transformer * the transformer to use, may not be null * @param outputCollection * the collection to output into, may not be null * @return the outputCollection with the transformed input added * @throws NullPointerException * if the output collection is null */ public static <I, O, C extends Collection<O>> C collect( Iterable<I> inputCollection, final Transformer<? super I, ? extends O> transformer, final C outputCollection) { if (inputCollection != null) { return collect(inputCollection.iterator(), transformer, outputCollection); } return outputCollection; }
/** * Returns a new Collection consisting of the elements of inputCollection * transformed by the given transformer. * <p/> * If the input transformer is null, the result is an empty list. * * @param inputCollection * the collection to get the input from, may not be null * @param transformer * the transformer to use, may be null * @return the transformed result (new list) * @throws NullPointerException * if the input collection is null */ public static <I, O> Collection<O> collect(Collection<I> inputCollection, Transformer<? super I, ? extends O> transformer) { ArrayList<O> answer = new ArrayList<O>(inputCollection.size()); collect(inputCollection, transformer, answer); return answer; }
Collection<E> resultCollection = collect(collection, transformer); collection.clear();