2

I have a class

Class Employee {

    String company;
    String deptId;
    Double salary;

    // other fields
    // constructors
    // setters n getters      
} 

I then somehow get the List of this class :

List<Employee> employees = getAllEmployees();

Now i want the output as a map.

Map<String, Map<String, Double>> companyVsDeptVsMaxSalary = employees.stream()........

So I want to have a Map where my key is the company and values as an other Map.

Now that other inner Map is a Map of dept vs maximum salary in that dept.

I can do that using boiler plate code.

But need to explore java 8 for concise n better code. I tried collectingAndThen n other things but couldn't get that right.

Appericiate the help. Thanks.

1
  • This is a specific question i feel. It has an input and requires an output. I'll still try to to be more careful on future.
    – DarkKnight
    Dec 18, 2018 at 14:03

2 Answers 2

0

You are looking to group by the company followed by another grouping by depthId and finally, select the maximum salary of the inner grouping.

This can be done via a groupingBy and reducing collectors:

Map<String, Map<String, Double>> result = 
     employees.stream()
              .collect(groupingBy(Employee::getCompany,
                              groupingBy(Employee::getDeptId,
                                     reducing(0D, Employee::getSalary,
                                             maxBy(comparingDouble(e -> e))))));

imports:

import java.util.stream.*;
import static java.util.stream.Collectors.*;
import static java.util.function.BinaryOperator.maxBy;
import static java.util.Comparator.*;
0

This could do the trick:

Map<String, Map<String, Double>> map =
    employees.stream().collect(
        groupingBy(Employee::getCompany,
            groupingBy(Employee::getDeptId,
                 collectingAndThen(
                     maxBy(comparing(Employee::getSalary)),
                     o -> o.get().getSalary())));

The idea of this code snippet is to map your employees by company first then by deptId. Finally we calculate max salary per company and deptId using Collectors.collectingAndThen and Collectors.maxBy methods.

5
  • 1
    Well, can anyone of downvoters provide some feedback?
    – ETO
    Dec 18, 2018 at 7:29
  • Didn't downvote, but anyway wouldn't it better to just do o -> o.get().getSalary() ? instead of o -> o.isPresent() ? o.getSalary() : 0.0 as there will always be at least one item in each group. if the source is empty then you'd never get to that part anyway.
    – Ousmane D.
    Dec 18, 2018 at 7:54
  • 1
    @Aomine Yes, you're right. Actually the very first version of my answer was doing so. Reverted it back.
    – ETO
    Dec 18, 2018 at 7:58
  • Just an update collectingAndThen and maxBy are methods of Collectors and not Comparator.
    – DarkKnight
    Dec 18, 2018 at 14:37
  • @DarkKnight Yes, it's just a typo. Fixed it.
    – ETO
    Dec 18, 2018 at 14:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.