为了账号安全,请及时绑定邮箱和手机立即绑定

Java 8:如何从列表创建映射,其中键从同一类(empID)获取,值作为对象(Employee)

Java 8:如何从列表创建映射,其中键从同一类(empID)获取,值作为对象(Employee)

温温酱 2024-01-25 15:22:07
如何在 Java 8 中从列表转换为映射的形式是empID从类内转换,并将值作为对象本身。List<CompanyEntity> result =               ifEntityManager.createNamedQuery("findByEmployeeId", CompanyEntity.class)              .getResultList(); Map<String, CompanyEntity> result1 = result.stream()         .collect(Collectors.toMap(CompanyEntity::getEmployeeId, CompanyEntity::this)); ??我想要第二个参数作为toMap对象本身。谁能建议如何实现同样的目标?我尝试这样做Collectors.toMap(CompanyEntity::getEmployeeId, this),但无法摆脱编译错误。
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

您可以Function.identity()使用java.util.function

Map<String, CompanyEntity> result1 = result.stream()
    .collect(Collectors.toMap(CompanyEntity::getEmployeeId, Function.identity()));


查看完整回答
反对 回复 2024-01-25
  • 1 回答
  • 0 关注
  • 31 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信