Reputation: 57
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.
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
Upvotes: 2
Views: 2245
Reputation: 39
You may use Map to solve your problem. You can have a Map< String, List< Employee>> which will hold "Department" as key and all Employee object which belongs to that department as value.
Then you will have to iterate over each department's employee list and count active and inactive employee.
Upvotes: 0
Reputation: 16908
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.
Upvotes: 1
Reputation: 344
This should do the trick...
List<Employee> listEmployee = new ArrayList<>();
listEmployee.add(new Employee("Ravi", "IT", "active"));
listEmployee.add(new Employee("Tom", "Sales", "inactive"));
listEmployee.add(new Employee("Kanna", "IT", "inactive"));
Map<String, Map<String, List<Employee>>> result = listEmployee.stream()
.collect(groupingBy(Employee::getDepartment, groupingBy(Employee::getStatus)));
result.forEach((department, departmentMap) -> {
System.out.println(department + ", "
+ departmentMap.size() + ", "
+ ofNullable(departmentMap.get("active")).orElse(emptyList()).size() + ", "
+ ofNullable(departmentMap.get("inactive")).orElse(emptyList()).size());
});
Upvotes: -1
Reputation: 41
It might not be the best approach but you can try this. Create a HashMap with Department as key and value will be a list of employees.
HashMap<String, List<Employee>> hashMap = new HashMap<Integer, List<Employee>>();
Iterate over the listEmployee and add all the employees to the hashmap with unique Department.
if (!hashMap.containsKey(e.getDepartment())) {
List<Employee> list = new ArrayList<Employee>();
list.add(e);
hashMap.put(e.getDepartment(), list);
} else {
hashMap.get(e.getDepartment()).add(e);
}
After creation of hashmap, you simply need to traverse over list in hashmap for every department to get the inactive and active students.
Size of List for every department will give you the total employees in that department.For this you can use this:
hashMap.get(e.getDepartment()).size()
Upvotes: 0
Reputation: 54168
You should use a Map
to group your employees depending of thei departement, then for each departement print the number of employee and the actives one, like this
/* for the collector
import static java.util.stream.Collectors.groupingBy;*/
Map<String, List<Employee>> employeePerDep =
listEmployee.stream().collect(groupingBy(Employee::getDepartement));
System.out.printf("%10s %10s %10s %10s\n", "Departement", "total", "active", "inactive");
for (Map.Entry<String, List<Employee>> entry : employeePerDep.entrySet()) {
int total = entry.getValue().size();
long active = entry.getValue().stream().filter(e -> e.active.equals("active")).count();
System.out.printf("%-10s %10d %10s %10s\n", entry.getKey(), total, active, total - active);
}
/* And get :
Departement total active inactive
Sales 1 0 1
IT 2 1 1
Improvement
If your String
for active, can only be active
or inactive
you should use a boolean, and make theses changes :
//attribute
private boolean active;
//instanciate
new Employee("Kanna", "IT", false);
//count
long active = entry.getValue().stream().filter(Employee::isActive).count();
//getter
public boolean isActive() {
return active;
}
DEMO : DEMO
Upvotes: 1