Java examples for Lambda Stream:Collector
Implement the collect function on list<T>
import java.util.List; import java.util.stream.Collector; public class Main { /**//from w w w . j a v a 2 s . co m * Implement the collect function on list<T>. The result should be equivalent to * the execution of the code: * * <pre> * list.stream().collect(col); * </pre> * * * @param list * @param col * a collector of type Collector<T,A,R> * @return a value of type R. */ public static <T, A, R> R collect(List<T> list, Collector<T, A, R> col) { // The result should be equivalent to the code: // list.stream().collect(col) ; A result = col.supplier().get(); list.forEach(t -> col.accumulator().accept(result, t)); return col.finisher().apply(result); } }