用于汇总float Java 8的定制收集器实现

I am trying to create a custom float addition similar to Collectors.summingDouble().

但是我面临2个问题,我不确定如何解决。

  1. BiConsumer - Line #27 - void methods cannot return a value
  2. Collectors.of - Line#32 - The method of(Supplier, BiConsumer<R,T>, BinaryOperator<R>, Collector.Characteristics...) in the type Collector is not applicable for the arguments (Supplier<Float[]>, BiConsumer<Float,Employee>, BinaryOperator<Float>) What needs to be done here for fixing the issue?
package src.java8.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Collector;
public class CustomCollector {

    public static void main(String[] args) {
Employee e1=new Employee(1,"Tom",10.2f);
Employee e2=new Employee(1,"Jack",10.4f);
Employee e3=new Employee(1,"Harry",10.4f);
        ArrayList<Employee> lstEmployee=new ArrayList<Employee>();
        lstEmployee.add(e1);lstEmployee.add(e2);lstEmployee.add(e3);

/*  Implementation 1
 *  double totalSal=lstEmployee.stream().collect(Collectors.summingDouble(e->e.getSal()));
        System.out.println(totalSal);
*/  
        //Implementation 2
        Function<Employee,Float> fun=(e)->e.getSal();
        BiConsumer<Float,Employee> consumer=(val,e)->val+e.getSal();
        BinaryOperator<Float> operator=(val1,val2)->val1+val2;
        Supplier<Float[]> supplier=() -> new Float[2];

        float FtotalSal=lstEmployee.stream().collect(
                Collector.of(supplier,consumer,operator));
        System.out.println(FtotalSal);
    }

}
class Employee
{
    int id;
    String name;
    float sal;

    public float getSal() {
        return sal;
    }

    public void setSal(float sal) {
        this.sal = sal;
    }

    Employee(int id,String name,float sal)
    {
        this.id=id;
        this.name=name;
        this.sal=sal;
    }

}
评论