[英]Java 8: How to create map from list where Key is fetched from same class (empID) and value as object(Employee) 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)); ??
我希望toMap
中的第二個參數作為 object 本身。 任何人都可以建議如何實現相同的目標嗎?
我嘗試做Collectors.toMap(CompanyEntity::getEmployeeId, this)
但無法擺脫編譯錯誤。
您可以使用 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.