Search code examples
javajava-8java-streamcollectors

Why Collectors.partitioningBy returning Object instead of Map


I have created a set of Predicates based on different attributes of my Employee class and used the default methods - and/or of Predicate to combine them to a single predicate and used this combined predicate in Collectors.partitioningBy method.

While the code compiles and executes fine on eclipse, building my project is giving me error as :

incompatible types: java.lang.Object cannot be converted to java.util.Map<java.lang.Boolean,java.util.List<com.MyProj.Employee>>

Predicate<Employee> addressSectionNull = emp-> emp== null || emp.getAddress() == null;
Predicate<Employee> isBlankLane = emp -> StringUtils.isBlank(emp.getAddress().getLane());
Predicate<Employee> notValidCity = emp -> "NA".equalsIgnoreCase(emp.getAddress().getCity());


Predicate notEligible =
    addressSectionNull.or(isBlankLane).or(notValidCity);
Map<Boolean, List<Employee>> empValidityMap =
    employees.parallelStream().collect(Collectors.partitioningBy(notEligible));

Solution

  • Change

    Predicate notEligible = ...
    

    to

    Predicate<Employee> notEligible = ...
    

    Reason: Collectors.partitioningBy(T predicate) will return a Collector<T, ?, Map<Boolean, List<T>>>. When your Predicate does not have a generic type, T will be Object.