Posts

Showing posts from April, 2016

All about Java stream collect offering

Lets start with a problem statement “Provided collection of employees, we need to find average, max and min age of employee by county” public static void avgMaxMinEmployeeAgeByCounter() { String AVG = "AVG"; String MAX = "MAX"; String MIN = "MIN"; String SUM = "SUM"; String COUNT = "COUNT"; List<Employee> employees = Employee.EMPLOYEES;//LIst of all employees, pojos // Mapping country name to average, max, min age of employee Map<String, Map<String, Double>> summary = new HashMap<>(); for(Employee e: employees) { if(summary.containsKey(e.getCountry())){ Map<String, Double> map = summary.get(e.getCountry()); map.put(SUM, (map.get(SUM) + e.getAge())); map.put(COUNT, map.get(COUNT) +1); map.put(MAX, map.get(MAX) < e.getAge() ? e.getAge():map.get(MAX)); map.put(MIN, map.get(MIN) > e.getAge() ? e.ge