如何在 Java 8 中從列表轉(zhuǎn)換為映射的形式是empID從類內(nèi)轉(zhuǎn)換,并將值作為對象本身。List<CompanyEntity> result =
ifEntityManager.createNamedQuery("findByEmployeeId", CompanyEntity.class)
.getResultList();
Map<String, CompanyEntity> result1 = result.stream()
.collect(Collectors.toMap(CompanyEntity::getEmployeeId, CompanyEntity::this)); ??我想要第二個參數(shù)作為toMap對象本身。誰能建議如何實現(xiàn)同樣的目標?我嘗試這樣做Collectors.toMap(CompanyEntity::getEmployeeId, this),但無法擺脫編譯錯誤。
1 回答

搖曳的薔薇
TA貢獻1793條經(jīng)驗 獲得超6個贊
您可以Function.identity()
使用java.util.function
:
Map<String, CompanyEntity> result1 = result.stream() .collect(Collectors.toMap(CompanyEntity::getEmployeeId, Function.identity()));
添加回答
舉報
0/150
提交
取消