I have a collection where employees from all departments of the office are kept. I need to split this one collection into several different collections, that is, I want to split all employees into their departments. It is important that I do not write the names of the departments myself, and the program itself breaks the collection into as many parts as necessary. Is it possible to do this, and if so, what would be the implementation?
There is a collection and this collection has office workers (all). The employee in the class has a registered department in which he works(Type STRING). It is necessary to divide one entire collection based on the departments.
To my regret I cannot share the classes (production code). I tried to run ArrayList through a for loop, and then split through an if, but then I got stuck and did not understand how other collections would then be created.
Employee:
public class Employee { private String name; private String lastName; private String department; } Method(where I spleet my lists):
public void method() { // Change method name List<EmployeeKnowledge> employeeKnowledgeList = employeeKnowledgeRepository .findEmployeeKnowledgeByStatus(KnowledgeStatus.KNOW); List<Employee> employeeList = new ArrayList<>(); // employeeKnowledgeList.forEach(e -> employeeList.add(employeeRepository.getOne(e.getEmployeeId()))); //AND NOW I DONT KNOW HOW SPLIT MY employeeList }