private static void salarySum(IMap<String, Employee> employees) { // create the PropertyExtractor to extract salary value from the employee PropertyExtractor<Employee, Integer> propertyExtractor = new SalaryPropertyExtractor(); // select all employees Supplier<String, Employee, Integer> supplier = Supplier.all(propertyExtractor); // choose the aggregation to perform Aggregation<String, Integer, Integer> aggregation = Aggregations.integerSum(); // execute the aggregation and print the result int sumSalary = employees.aggregate(supplier, aggregation); System.out.println("Sum of all salaries: " + sumSalary); System.out.println("\n"); // in Java 8: //int sumSalary = employees.aggregate(Supplier.all((value) -> value.getSalaryPerMonth()), Aggregations.integerSum()); }
@Override public void execute(HazelcastInstance hazelcastInstance) throws Exception { IMap<String, SalaryYear> map = hazelcastInstance.getMap("salaries"); Supplier<String, SalaryYear, Integer> supplier = Supplier.all(new SalaryPropertyExtractor()); Aggregation<String, Integer, Integer> aggregation = Aggregations.integerSum(); int sum = map.aggregate(supplier, aggregation); System.out.println("Salary sum: " + sum); }