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 [])
我需要帮助。
您在第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()
。
基本上有两个问题 -
Function.identity()
之后你不能拥有;
。return
值。