代码之家  ›  专栏  ›  技术社区  ›  Bikash Das

collector.of()参数类型未按我希望的方式解析

  •  2
  • Bikash Das  · 技术社区  · 6 年前

    (供应商<R>供应商、双用户<R、T>收集器、二进制运算符<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, 类型collector中的函数、collector.characteristics…)是 不适用于参数(arraylist::new,)( 列表,元素)->,(列表1,列表2)-> ,函数,收集器。特征[])

    我需要帮助。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Eran    6 年前

    第3个参数(a)中缺少返回值 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() .

        2
  •  3
  •   Naman    6 年前

    基本上有两个问题-

    1. 之后出现语法错误 Function.identity() 你不能 ; .

    2. 缺少第三个参数,它是 return 价值。