简体   繁体   中英

How to use collect method with three params i.e "collect(supplier, accumulator, combiner)" to add integers in a list

Being a beginner to java functional programming, I need some help on collect() that takes into consideration the three params ie list.parallelStream().collect(supplier, accumulator, combiner) . How can i calculate sum of salary all employees mentioned in the list.

public class SumMethodsOfCollectorclass {
    public static void main(String[] args) {
      List<Employee> list = new ArrayList<>();
      list.add(new Employee(6, "Nick", 27, "Software Engineer", 44000f));
      list.add(new Employee(9, "Tom", 23, "Civil Engineer", 32000f));
      list.add(new Employee(3, "Jon", 29, "Mechanical Engineer", 37000f));
      list.add(new Employee(4, "Harry", 21, "Surgeon", 55000f));
      list.add(new Employee(8, "Don", 25, "Laywer", 50000f));
      list.add(new Employee(7, "Marry", 20, "Police", 29000f));
      list.add(new Employee(2, "Angel", 22, "Professor", 35000f));
      list.add(new Employee(1, "Kate", 23, "Teacher", 29000f));
      list.add(new Employee(5, "Evan", 22, "Pilot", 44000f));

      sumOfAgeOfAllEmployees(list);
      sumOfSalaryOfAllEmployees(list);
    }

    private static void sumOfAgeOfAllEmployees(List<Employee> list) {
      Integer result = list.stream().parallel().collect(Collectors.summingInt(i -> i.getAge()));
      System.out.println("The sum of age of all employees - " + result);
    }

    private static void sumOfSalaryOfAllEmployees(List<Employee> list) {
      // to get the sum of salary of all the employees
      list.parallelStream().collect(supplier, accumulator, combiner)
    }
}

You can perform this mutable reduction using an atomic integer. This may mean additional cost in a sequential stream, but here's how it would work:

private static int sumOfSalaryOfAllEmployees(List<Employee> list) {
    AtomicInteger res = list.stream().mapToInt(Employee::getSalary)
            .collect(AtomicInteger::new, 
                     AtomicInteger::addAndGet, 
                     (ai1, ai2) -> ai1.addAndGet(ai2.get()));

    return res.get();
}

You may also want a custom class to use for the accumulation of values (if synchronization in AtomicInteger is unnecessary overhead):

private static class IntHolder {
    private int value;

    public IntHolder() {
    }

    public IntHolder(int val) {
        this.value = val;
    }

    public int getValue() {
        return value;
    }

    public void add(int val) {
        this.value += val;
    }

    public void accumulate(IntHolder other) {
        this.value += other.value;
    }
}

Then:

private static int sumOfSalaryOfAllEmployees(List<Employee> list) {
    IntHolder res = list.stream().mapToInt(Employee::getSalary)
        .collect(IntHolder::new, IntHolder::add, IntHolder::accumulate);

    return res.getValue();
}

Basically, your supplier needs to return a mutable result container.

An AtomicInteger will do, or an custom IntHolder class. Or a MutableInt from apache commons-lang3:

    MutableInt sum = list.parallelStream().collect(
            () -> new MutableInt(0),
            (sum1, employee) -> sum1.add(employee.getSalary()),
            (sum1, sum2) -> sum1.add(sum2.intValue())
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM