Bikash Das
Bikash Das

Reputation: 147

Collector.of() arguments types are not resolved the way I want

Collector.of(Supplier<R> supplier, BiConsumer<R,T> accumulator, BinaryOperator<R> combiner, Function<R> finisher, Characterstics...)

    Collector<Integer, List<Integer>, List<Integer>> myCollector = 
            Collector.of(ArrayList<Integer>::new, 
                    (list, element) -> {list.add(element);}, 
                    (list1, list2) -> {list1.addAll(list2);},  
                    Function.identity();, 
                    Characteristics.values()
                    );

When I run the above code I expected the types used in the static function Collector.of() will be resolved but it wasn't. It invokes the following error in eclipse

The method of(Supplier, BiConsumer, BinaryOperator, Function, Collector.Characteristics...) in the type Collector is not applicable for the arguments (ArrayList::new, ( list, element) -> {}, ( list1, list2) -> {}, Function, Collector.Characteristics[])

I need help with this.

Upvotes: 2

Views: 177

Answers (2)

Raj
Raj

Reputation: 727

Basically two issues -

  1. A syntax error you are having, after Function.identity() you can't have ;.

  2. Third parameter is missing which is a return value.

Upvotes: 3

Eran
Eran

Reputation: 394126

You are missing a return value in the 3rd parameter (a BinaryOperator must have a return value):

Collector<Integer, List<Integer>, List<Integer>> myCollector = 
        Collector.of(ArrayList<Integer>::new, 
                (list, element) -> {list.add(element);}, 
                (list1, list2) -> {list1.addAll(list2); return list1;}, 
                                                    //  -------------   added 
                Function.identity(), 
                Characteristics.values()
                );

You also had an extra ; after Function.identity().

Upvotes: 2

Related Questions