How to return the result of a map? Are there any predefined collectors?

Dhananjay Nene dhananjay.nene at
Fri Feb 22 14:08:20 PST 2013

I am using build b78

The result of a sequence of map operations is a Stream<T>. However I needed
to cast the functions explicitly as follows

Stream<Integer> doubles =
Arrays.asList(1,2,3,4).stream().map((Function<Integer, Integer>) n -> n *
2).map((Function<Integer, Integer>) n -> n + 1);

Q1. Is there a way to avoid the cast Function<Integer, Integer> here?

Also I wanted to convert the stream into a List. I understood the way to do
so is using a collector. Google searches led me to believe there is a
function toList() which should help. However I couldn't find a method
"toList" on Stream. Neither could I easily locate it anywhere in the java

Q2. Are there any predefined collectors I could use ? eg. in this case to
convert a Stream<Int> into a List<Int>

I did take a shot at implementing a collector as follows :

public class ListCollector<T> implements Collector<T, List<T>> {
    class ListSupplier<T> implements Supplier<List<T>> {
        private List<T> list = new LinkedList<T>();
        public List<T> get() {
            return this.list;

    class ListBiConsumer<T> implements BiConsumer<List<T>, T> {
        public void accept(List<T> ts, T t) {

    public class ListBinaryOperator<T> implements BinaryOperator<List<T>> {
        public List<T> apply(List<T> ts, List<T> ts2) {
            return ts;
    private ListSupplier<T> supplier = new ListSupplier<T>();
    private BiConsumer<List<T>,T> accumulator = new ListBiConsumer<T>();
    private BinaryOperator<List<T>> combiner = new ListBinaryOperator<T>();

    public Supplier<List<T>> resultSupplier() {
        return this.supplier;

    public BiConsumer<List<T>, T> accumulator() {
        return this.accumulator;

    public BinaryOperator<List<T>> combiner() {
        return combiner;


Q3. Is that a reasonable implementation of a collector. I was particularly
unsure of the scope at which a collector gets created and destroyed. Is
declaring (and instantiating) a LinkedList once per instance of collector


More information about the lambda-dev mailing list