I have 2 classes and contains the below property languageMap
class Person {
Map<String, String> languageMap;
}
class Employee {
Map<String, String> languageMap;
}
There are two methods addressMap(List<Person> persons) and employeeMap(List<Employee> employee) it calls Function interface,
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));
}
I wanted to make the 2 buildXXX() methods to be common, and tried to use Generics as below,
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));
}
any Generics or Stream technique to overcome the issue?