gpt4 book ai didi

java - 我如何使用计数处理ArrayList,并按函数分组,并在值中包含字符串数组

转载 作者:行者123 更新时间:2023-11-30 06:01:31 27 4
gpt4 key购买 nike

我正在处理员工的数组列表,需要按员工数量按函数使用情况进行分组,计算活跃员工和非活跃员工。我知道如何处理总数,但是当值位于数组字符串中时,即一名员工与多个部门关联时,如何使用按函数分组处理数组列表。

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);

这是我试图获取员工数量的上述代码

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

请帮我按部门分组处理数据

我期待以下输出:

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

最佳答案

首先我们需要创建一个类来保存每个部门的计数器。
类似于下面的预兆(为了简洁起见,实例变量被设为公共(public))。需要 equals()hashCode() 因为我们要将此类的实例放入 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();
}
}

其次,我们将创建一个 map ,允许我们按部门对员工进行分组并保存累积计数器

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

现在,迭代员工列表是一个简单的问题,对于每个项目,迭代部门列表,从 map 中获取适当的条目并累积计数器(并放回到 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);
}
}

关于java - 我如何使用计数处理ArrayList,并按函数分组,并在值中包含字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57391687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com