Map<String,Map<String,Integer> return in java 8

Sanjay Singh
Sep 9, 2023

I have a list and that is student type and student contain (id,name, department,gender) on basis of department how to get employee gender count Like

o/p

HR male 20 female 30

Development male 20 female 60

{Product Development={Female=1, Male=4},
Security And Transport={Male=2},
Sales And Marketing={Female=1, Male=2},
Infrastructure={Female=1, Male=2},
HR={Female=2},
Account And Finance={Female=1, Male=1}}

solution

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EmployeeLogic {

public static void main(String[] args) {

List<Employee> list = SetDataForEmployee.getEmployeeData();

Map<String, Map<String, Long>> counts =
list.stream().
collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.groupingBy(Employee::getGender, Collectors.counting())));


System.out.println(counts);
}

}

--

--

Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA