How can i process the ArrayList with count, and group by function

cb rak :

enter image description hereI 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.

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

ArrayList<Employee> listEmployee = new ArrayList<Employee>();
listEmployee.add(new Employee("Ravi", "IT", "active"));
listEmployee.add(new Employee("Tom", "Sales", "inactive"));
listEmployee.add(new Employee("Kanna", "IT", "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         2     1           1
Sales      1     0           1
Fullstack Guy :

You can use the stream() method from the List<Employee> to get a Stream<Employee> and use the Collectors.groupingBy(Employee::getDepartment) to group the Employee objects by the department. Once that is done you would get back a Map<String, List<Employee>> map object.

The key will be the department name and the value will be a list of Employee objects, now from that list of employees we can further filter the inactive and active employees:

System.out.println("Department total activeCount inactiveCount");
listEmployee.stream().collect(Collectors.groupingBy(Employee::getDepartment)).forEach((dept, emps) -> {
     int count = emps.size();
     long activeCount = emps.stream().filter(e -> "active".equals(e.getActive())).count();
     long inactiveCount = emps.stream().filter(e -> "inactive".equals(e.getActive())).count();
     int i = 12 - dept.length();
     System.out.format(dept + "%" + i +"s" + count + "%10s" + activeCount + "%10s" + inactiveCount, " ", " ", " ");
     System.out.println();
 });

Output:

Department total activeCount inactiveCount
Sales       1          0          1
IT          2          1          1

It is also advisable to use an Enum for the active or inactive status rather than a String.

Guess you like

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