방법 I의 값은 문자열 배열을 갖는 기능에 의해 카운트의 ArrayList와 그룹을 처리 할

cbbeginner :

나는 직원의 ArrayList를 처리, 직원의 수에 의해 기능을 사용하여 그룹을 필요 활성 직원을 계산하고 비활성 직원을 계산했다. 내가 총을 처리하는 방법을 알고 있지만 값이 배열 문자열에있을 때 내가 어떻게이 기능에 의해 그룹과의 ArrayList를 처리 할 수있는, 즉, 한 직원이 여러 부서와 연결되어 있습니다.

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<String, DepartmentStats> departmentStatsMap = new HashMap<>();

지금, 그것은 직원의리스트를 각 항목에 대한 반복의 간단한 문제가 반복, 부서의리스트를지도에서 해당 항목을 가지고 카운터를 축적 (맵에 다시 넣어) :

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

추천

출처http://43.154.161.224:23101/article/api/json?id=231858&siteId=1