java - 为什么 Collectors.partitioningBy 返回 Object 而不是 Map
问题描述
我创建了一组基于 Employee 类的不同属性的谓词,并使用默认方法和/或 Predicate 将它们组合成一个谓词,并在 Collectors.partitioningBy 方法中使用这个组合谓词。
虽然代码在 Eclipse 上编译和执行良好,但构建我的项目却给了我错误:
不兼容的类型:java.lang.Object 无法转换为 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));
解决方案
改变
Predicate notEligible = ...
至
Predicate<Employee> notEligible = ...
原因:Collectors.partitioningBy(T predicate)
将返回一个Collector<T, ?, Map<Boolean, List<T>>>
. 当您的 Predicate 没有泛型类型时, T 将是Object
.
推荐阅读
- c++ - std::unique_ptr 分配和读取线程对于控制块是否安全?
- java - 如何从另一个包创建列表类型的非公共类?
- haskell - 如何将 Aeson 对象解析为我自己的自定义类型?
- angular - 在 Angular 中的页面加载时从数据库中获取数据
- r - 无法加载 RWeka 包
- javascript - React 媒体编辑器 - 如果单击占位符,需要单击两次才能编写
- python - 在 python 中使用 pint 以米和公里结束
- snowflake-cloud-data-platform - 连接到 URL 的 Snowflake REST 请求失败时出错
- python - leetcode 中的 sumtwo 问题:嗨,我是 Leetcode 的新手,我正在尝试解决第一个问题
- javascript - 字符串格式的 JavaScript 函数