在键重复的情况下,调整可能是合并功能,可以覆盖现有值:
allEmployees.stream()
.collect(Collectors.toMap(CompanyEntity::getEmployeeId,Function.identity(),(a,b) -> b))
但是将现有的实现定义为引用的会更好很多,我检查其中是否包含并更新Map 。如果您检查是否存在并且不更新(put
),则应改用(a,b) -> a
。
,
您可以使用分组收集器。
这将为您提供唯一的employeeId值和事件列表。
在groupingby收集器的第二步中,您可以获得列表的第一个元素
也可以看看
Stream groupingBy: reducing to first element of list
和
https://www.baeldung.com/java-groupingby-collector
,
类似的东西对我有用:
// this.fsRepo.getDosIdsWithDcs(dosIds)基本返回一个List
return this.fsRepo.getDosIdsWithDcs(dosIds).
stream().collect
(Collectors.toMap(YourModelDosClass::getDosId,v->v,(v1,v2) -> v1));
本文链接:https://www.f2er.com/3145830.html