У сотрудника есть атрибут «Имя, возраст, отдел, город».
Теперь у меня есть список другого списка.
Список Критерии могут быть динамическими, поскольку могут содержать один или несколько критериев. Я хочу отфильтровать список сотрудников на основе этого динамического списка критериев, поэтому, если тип — возраст, мне нужно сравнить возраст с emp.age, а если тип — город, то мне нужно сравнить с emp.city.
Я не включая сюда геттеры/сеттеры. А вот как выглядят занятия:
Код: Выделить всё
public class Person {
private List empList;
public Person(List empList) {
super();
this.empList = empList;
}
public List getEmpList() {
return empList;
}
public void setEmpList(List empList) {
this.empList = empList;
}
}
Код: Выделить всё
public class Employee {
private String name;
private int age;
private String city;
private String dept;
public Employee(String name, int age, String city, String dept) {
super();
this.name = name;
this.age = age;
this.city = city;
this.dept = dept;
}
}
Код: Выделить всё
public class Criteria {
private CriteriaType type;
private String value;
public Criteria(CriteriaType type, String value) {
super();
this.type = type;
this.value = value;
}
}
Код: Выделить всё
public enum CriteriaType {
NAME, CITY, AGE, DEPT
}
Код: Выделить всё
public class FilterEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee("John", 30, "Dallas", "HR");
Employee emp2 = new Employee("Steve", 31, "Austin", "HR");
Employee emp3 = new Employee("Andrew", 25, "Houston", "Finance");
Employee emp4 = new Employee("Mike", 30, "Dallas", "HR");
List empList = new ArrayList();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
Person personObj = new Person(empList);
Criteria c1 = new Criteria(CriteriaType.CITY, "Dallas");
Criteria c2 = new Criteria(CriteriaType.NAME, "Mike");
List criteriaList = new ArrayList();
criteriaList.add(c1);
criteriaList.add(c2);
//Filter person list containing employee list on the basis of //Criteria list
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... va-streams
Мобильная версия