我正在处理员工的数组列表,并且需要按功能使用情况(按员工人数,活跃员工人数和非活跃员工人数进行分组)进行分组。我知道如何处理总数,但如何处理按功能分组的arraylist。
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);
这是我试图获取员工人数的上述代码
int count = 0;
for (Employee e : listEmployee) {
count++;
}
System.out.println("Count of Employees" + count);
请按部门分组帮助我处理数据
我期望以下输出:
Department total activeCount inactiveCount
IT 2 1 1
Sales 1 0 1
答案 0 :(得分:1)
您应该使用Map
根据部门对其雇员进行分组,然后为每个部门打印雇员和活动人员的数量,就像这样
/* 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
改进
如果您的String
处于活动状态,只能是active
或inactive
,则应使用布尔值并进行以下更改:
//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
答案 1 :(得分:1)
您可以使用stream()
中的List<Employee>
方法来获取Stream<Employee>
,并使用Collectors.groupingBy(Employee::getDepartment)
按部门对Employee对象进行分组。完成此操作后,您将返回一个Map<String, List<Employee>>
地图对象。
键将是部门名称,值将是Employee
对象的列表,现在从该员工列表中,我们可以进一步过滤不活跃和活跃员工:
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();
});
输出:
Department total activeCount inactiveCount
Sales 1 0 1
IT 2 1 1
建议将 Enum 用于活动或非活动状态,而不是字符串。
答案 2 :(得分:0)
这可能不是最好的方法,但是您可以尝试一下。 创建一个以Department为键的 HashMap ,值将是员工列表。
HashMap<String, List<Employee>> hashMap = new HashMap<Integer, List<Employee>>();
遍历 listEmployee ,并将所有雇员添加到具有唯一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);
}
创建哈希图后,您只需要遍历哈希图中每个部门的列表即可获得不活跃和活跃的学生。
每个部门的列表大小将为您提供该部门的员工总数。为此,您可以使用以下方法:
hashMap.get(e.getDepartment()).size()
答案 3 :(得分:0)
您可以使用Map解决您的问题。您可以具有一个 Map <字符串,列表<员工>> ,它将“ 部门”作为键,而属于该部门的所有Employee对象都作为 value < / strong>。
然后,您将需要遍历每个部门的员工列表,并计算活跃和不活跃的员工。
答案 4 :(得分:-1)
这应该可以解决问题...
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());
});