Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
initialiseMap
final Collection<Employee> employees = List.of(
new Employee("Karen Smith", 51200.0, 29, Employee.Sex.FEMALE),
new Employee("John Smith", 24000.0, 32, Employee.Sex.MALE),
new Employee("Anthony Jackson", 44000.0, 33, Employee.Sex.MALE),
new Employee("Alyson Palmer", 34320.0, 36, Employee.Sex.FEMALE),
new Employee("Jessica Sanders", 64320.0, 34, Employee.Sex.FEMALE)
);
final Map<Employee.Sex, List<Employee>> existingMap = new HashMap<>() {
{
put(Employee.Sex.MALE, new ArrayList<>() {{add(new Employee("Peter Parker", 28100.0, 33, Employee.Sex.MALE));}});
put(Employee.Sex.FEMALE, new ArrayList<>() {{add(new Employee("Sarah Williams", 18100.0, 19, Employee.Sex.FEMALE));}});
}
};
final Map<Employee.Sex, List<Employee>> employeesOverThirtyBySex = employees.stream()
.collect(groupingBy(Employee::getSex, () -> existingMap, filtering(employee -> employee.getAge() > 30, toList())));
System.out.println(employeesOverThirtyBySex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment