【问题标题】:Java Lambda Function Interface Extract to Common MethodsJava Lambda 函数接口提取到常用方法
【发布时间】:2021-05-10 02:10:10
【问题描述】:

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

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

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

有两个方法addressMap(List&lt;Person&gt; persons)employeeMap(List&lt;Employee&gt; 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));
  }

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

【问题讨论】:

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


    【解决方案1】:

    您可以让调用者为每种类型发送一个 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多