Collector.of()参数类型没有按我想要的方式解析

问题描述 投票:2回答:2

Collector.of(供应商<R>供应商,BiConsumer <R,T>累加器,BinaryOperator <R>合并器,功能<R>终结器,特征......)

    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()
                    );

当我运行上面的代码时,我预计静态函数Collector.of()中使用的类型将被解析,但事实并非如此。它在eclipse中调用以下错误

参数类型中的(Supplier,BiConsumer,BinaryOperator,Function,Collector.Characteristics ...)方法不适用于参数(ArrayList :: new,(list,element) - > {},(list1,list2) - > {},Function,Collector.Characteristics [])

我需要帮助。

java java-stream collectors
2个回答
2
投票

您在第3个参数中缺少返回值(BinaryOperator必须具有返回值):

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()
                );

;之后你还有一个额外的Function.identity()


3
投票

基本上有两个问题 -

  1. 你有一个语法错误,在Function.identity()之后你不能拥有;
  2. 缺少第三个参数,即return值。
© www.soinside.com 2019 - 2024. All rights reserved.