[英]Java 8: How to create map from list where Key is fetched from same class (empID) and value as object(Employee) itself?
How to convert from the list to map in Java 8 were in form of empID
from within class and value as an object itself.如何从列表转换为 Java 8 中的 map 是 class 中的
empID
形式,其值为 ZA2668CFEB89 本身。
List<CompanyEntity> result =
ifEntityManager.createNamedQuery("findByEmployeeId", CompanyEntity.class)
.getResultList();
Map<String, CompanyEntity> result1 = result.stream()
.collect(Collectors.toMap(CompanyEntity::getEmployeeId, CompanyEntity::this)); ??
I want the second parameter in toMap
as object itself.我希望
toMap
中的第二个参数作为 object 本身。 Could anyone suggest how to achieve the same?任何人都可以建议如何实现相同的目标吗?
I tried doing Collectors.toMap(CompanyEntity::getEmployeeId, this)
but not able to get away from compile errors.我尝试做
Collectors.toMap(CompanyEntity::getEmployeeId, this)
但无法摆脱编译错误。
You can use Function.identity()
from java.util.function
:您可以使用 java.util.function 中的
java.util.function
Function.identity()
:
Map<String, CompanyEntity> result1 = result.stream()
.collect(Collectors.toMap(CompanyEntity::getEmployeeId, Function.identity()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.