How can i process the ArrayList with count, and group by function, having string array in value

cbbeginner :

I was process the arraylist of employees, and need the group by function usage by count of employees, count active employees and count inactive employees. I know how to process the total, but how can i process the arraylist with group by function when value is in array string, i.e one employee is associated with multiple departments.

public class Employee {
    private String name;
    private List<String> department;
    private String status;
    public Employee(String name, List<String> department, String status) {
        this.setName(name);
        this.setDepartment(department);
        this.setStatus(status);
    }
    public String getName() {
        return name;
    }
    public List<String> getDepartment() {
        return department;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setDepartment(List<String> department) {
        this.department = department;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

ArrayList<Employee> listEmployee = new ArrayList<Employee>();

         List<String> listString1 = Arrays.asList("IT", "Sales");
         List<String> listString2 = Arrays.asList("Sales");
         List<String> listString3 = Arrays.asList("Sales");


         listEmployee.add(new Employee("Ravi", listString1, "active"));
         listEmployee.add(new Employee("Tom", listString2, "inactive"));
         listEmployee.add(new Employee("Kanna", listString3, "inactive"));

int count = 0;
for (Employee e : listEmployee) {
    count++;
}
System.out.println("Count of Employees" + count);

This is the above code i tried to get the count of employees

int count = 0;
for (Employee e : listEmployee) {
    count++;
}
System.out.println("Count of Employees" + count);

Please help me to process the data by grouping of department

I am expecting the following output to come:

Department total activeCount inactiveCount
IT         1     1           0
Sales      3     1           2
Sharon Ben Asher :

First we need to create a class that will hold the counters for each department.
Something like the bode below (instance variables were made public for brevity). equals() and hashCode() are needed since we are going to put instances of this class into a Map

public class DepartmentStats {
    public String name;
    public int total = 0;
    public int activeCount = 0;
    public int inactiveCount = 0;

    public DepartmentStats() {}
    public DepartmentStats(String name) {
        this.name = name;
    }

    /**
     * equality based on dept name
     */
    @Override
    public boolean equals(Object other) {
        return other instanceof DepartmentStats && 
               this.name.equals(((DepartmentStats) other).name);
    }

    /**
     * hash code based on dept name
     */
    @Override
    public int hashCode() {
        return name.hashCode();
    }
}

Second, we will create a map that will allow us to group employees by departments and hold the accumulating counters

Map<String, DepartmentStats> departmentStatsMap = new HashMap<>();

Now, it is a straightforward matter of iterating over the list of employees and for each item, iterate over the list of departments, take the appropriate entry from the map and accumulate the counters (and putting back into the map):

for (Employee employee : listEmployee) {
    for (String departmentName : employee.getDepartment()) {
        DepartmentStats departmentStats = departmentStatsMap.get(departmentName);
        if (departmentStats == null) departmentStats = new DepartmentStats(departmentName);
        departmentStats.total++;
        if (employee.getStatus().equals("active")) departmentStats.activeCount++;
        if (employee.getStatus().equals("inactive")) departmentStats.inactiveCount++;
        departmentStatsMap.put(departmentName, departmentStats);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127733&siteId=1