You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Collectors.partitioningBy返回Object而非Map<Boolean,List<Employee>>

错误根因
  • 你在声明组合谓词notEligible时使用了无泛型的原始类型Predicate,触发了泛型类型擦除,标准javac编译器无法识别该谓词的入参是Employee类型,因此认为partitioningBy返回的是无泛型的Map,和你声明的带泛型的Map<Boolean, List<Employee>>类型不匹配。
  • Eclipse自带的ECJ编译器对泛型校验规则更宽松,因此可以正常编译运行,但是标准javac在构建时会抛出类型不兼容错误。
解决方法

只需要给notEligible变量加上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<Employee>
Predicate<Employee> notEligible =
    addressSectionNull.or(isBlankLane).or(notValidCity);
Map<Boolean, List<Employee>> empValidityMap =
    employees.parallelStream().collect(Collectors.partitioningBy(notEligible));

提示:Java中所有带泛型的类都不建议直接使用原始类型声明,否则会触发全类的泛型擦除,引发意料之外的类型校验错误。

内容的提问来源于stack exchange,提问作者anik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 03:15:06