I am trying to create a custom float addition similar to Collectors.summingDouble().
But I am facing 2 issues and I am not sure about how to fix it.
BiConsumer- Line #27 - void methods cannot return a valueCollectors.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?
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;
// getters, setter, constructror
}