如何使用防止nullPointerException的辅助方法用法来过滤地图集合

时间:2019-09-18 09:16:34

标签: java functional-programming java-stream java-11

我有类似的方法:

static List<Employee> getEmployeesByFactoryAndTask(
      ProtectedMap<String, Factory> factoryMap, String factoryName, Task task) {

    return factoryMap.values().stream()
        .filter(factory -> factory.getName().equals(factoryName))
        .map(Factory::getEmployees)
        .flatMap(Optional::stream)
        .flatMap(Collection::stream)
        .filter(employee -> employee.getTask().equals(task))
        .collect(Collectors.toList());
  }

假定通过EmployeesfactoryName枚举参数过滤Task集合。

上面的代码可以正常工作,但是我想在我的ProtectedMap类中使用辅助方法find

public class ProtectedMap<T, U> extends HashMap<T, U> {
    public Optional<U> find(T key) {
        return Optional.ofNullable(super.get(key));
    }
}

以获得相同的结果。

到目前为止,我的方法如下:

return factoryMap
        .find(factoryName)
        .stream()
        .map(Factory::getEmployees)
        .flatMap(Optional::stream)
        .flatMap(Collection::stream)
        .collect(Collectors.toMap(Employee::getTask, Function.identity()));

但是我不知道该在第二个方法调用find中放置Task进行过滤以达到绝望效果。另外,Factory包含两个字段:Optional<List<Employees>>String,这是factory的名称。 对于如何解决问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

下面是处理您的要求的代码,但是getEmployeesByFactoryAndTask还返回Optional<List<Employee>>,据我所知,此返回值可以由您处理。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;


enum Task {
    QA, DEVELOPEMENT, DESIGN
}

class Employee {
    long id;
    String name;
    Task task;

    Employee(long id, String name, Task task) {
        this.id = id;
        this.name = name;
        this.task = task;
    }

    Task getTask() {
        return task;
    }
}

class Factory {
    List<Employee> employees;

    Factory() {}

    Factory(List<Employee> employees) {
        this.employees = employees;
    }

    public Optional<List<Employee>> getEmployees() {
        return Optional.ofNullable(employees);
    }
}

class ProtectedMap<T, U> extends HashMap<T, U> {
    public Optional<U> find(T key) {
        return Optional.ofNullable(super.get(key));
    }
}

public class Main57989215 {

    public static void main(String[] args) {
        List<Employee> employees1 = new ArrayList<>();
        employees1.add(new Employee(21, "Alen", Task.DESIGN));
        employees1.add(new Employee(31, "Herry", Task.QA));
        employees1.add(new Employee(41, "John", Task.DEVELOPEMENT));
        employees1.add(new Employee(51, "Martin", Task.QA));
        employees1.add(new Employee(61, "Jack", Task.DESIGN));


        List<Employee> employees2 = new ArrayList<>();
        employees2.add(new Employee(121, "John", Task.DESIGN));
        employees2.add(new Employee(131, "Porter", Task.QA));
        employees2.add(new Employee(141, "Cena", Task.DEVELOPEMENT));
        employees2.add(new Employee(151, "Luther", Task.QA));
        employees2.add(new Employee(161, "Dinga", Task.DESIGN));


        ProtectedMap<String, Factory> factoryMap = new ProtectedMap<>();
        factoryMap.put("factory1", new Factory(employees1));
        factoryMap.put("factory2", new Factory(employees2));
        factoryMap.put("factory3", new Factory());

        System.out.println(getEmployeesByFactoryAndTask(factoryMap, "factory1", Task.QA));
        System.out.println(getEmployeesByFactoryAndTask(factoryMap, "factory1", Task.QA));
        System.out.println(getEmployeesByFactoryAndTask(factoryMap, "factory2", Task.DEVELOPEMENT));
        System.out.println(getEmployeesByFactoryAndTask(factoryMap, "factory3", Task.DESIGN));
        System.out.println(getEmployeesByFactoryAndTask(factoryMap, "factory4", Task.QA));
    }

    static Optional<List<Employee>> getEmployeesByFactoryAndTask(ProtectedMap<String, Factory> factoryMap, String factoryName, Task task) {
        return Optional.ofNullable(factoryMap.find(factoryName)).orElse(Optional.empty())
                .flatMap(factory -> Optional.ofNullable(factory.getEmployees())
                        .orElse(Optional.empty()).map(employees -> employees.stream()
                                .filter(employee -> employee.getTask().equals(task)).collect(Collectors.toList())));
    }
}