Java Lambda 函数接口提取到常用方法

Java Lambda Function Interface Extract to Common Methods

我有 2 个 类 并且包含以下 属性 languageMap

class Person {
    Map<String, String> languageMap;
}

class Employee {
    Map<String, String> languageMap;
}

有两个方法addressMap(List<Person> persons)employeeMap(List<Employee> employee)调用Function接口,

public Map<String, String addressMap(List<Person> persons){
        Function<Collection<Person>,
                Map<String, String>> personFunc = CommonUtils::buildPersonMap;

               return personFunc.apply(persons);
  }

public Map<String, String employeeMap(List<Employee> employee){
    Function<Collection<Employee>,
         Map<String, String>> addressFunc = CommonUtils::buildEmployeeMap;

         return addressFunc.apply(employee);
  }

private static Map<String, String> buildPersonMap(Collection<Person> personItem) {
    return personItem.stream()
            .filter(element -> element.getLanguageMap() != null)
            .flatMap(element -> element.getLanguageMap()
                    .entrySet()
                    .stream())
            .filter(map -> map.getKey() != null && map.getValue() != null)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));
}

private static Map<String, String> buildEmployeeMap(Collection<Employee> employeeItem) {
    return employeeItem.stream()
            .filter(element -> element.getLanguageMap() != null)
            .flatMap(element -> element.getLanguageMap()
                    .entrySet()
                    .stream())
            .filter(map -> map.getKey() != null && map.getValue() != null)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));
}

我想让 2 buildXXX() 方法通用,并尝试使用如下泛型,

private static Map<String, String> buildMap(Collection<?> input) {

    return input.stream()
      .filter(element -> element.getLanguageMap() != null). // ERROR
      .flatMap(element -> element.getLanguageMap().entrySet().stream()) // ERROR
      .filter(map -> map.getKey() != null && map.getValue() != null)
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));
  }

任何泛型或流技术可以解决这个问题?

您可以让调用者为每种类型发送 getter,并以这种方式实现您的方法:

private static <T> Map<String, String> buildMap(Collection<T> input,
        Function<T, Map<String, String>> languageMapGetter) {

    return input.stream()
            .map(languageMapGetter) //you can also call it yourself
            .filter(Objects::nonNull)
            .flatMap(element -> element.entrySet().stream())
            .filter(map -> map.getKey() != null && map.getValue() != null)
            .collect(Collectors.toMap(Map.Entry::getKey, 
                    Map.Entry::getValue, (a, b) -> a));
}

这将使您的特定于类型的方法如下所示:

public Map<String, String> addressMap(List<Person> persons) {
    return CommonUtils.buildMap(persons, Person::getLanguageMap);
}

public Map<String, String> employeeMap(List<Employee> employee) {
    return CommonUtils.buildMap(employee, Employee::getLanguageMap);
}