Man*_*sal 1 java lambda java-8 java-stream
我正在尝试从列表创建一个 Map,而 Map 的值将是某种转换的结果,这就是我的代码的样子。
empIdList.stream()
.map(id-> getDepartment(id))
.collect(Collectors.toMap(id, department:String -> department)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我希望将其id用作键和department值。你能帮我实现我的预期结果吗?
您可以避免该map功能并直接调用collect
empIdList.stream()
.collect(Collectors.toMap(Function.identity(), this::getDepartment);
Run Code Online (Sandbox Code Playgroud)
如果列表没有唯一的 id,那么它会导致IllegalStateException这样:
Setids 而不是 a List。distinct()在stream()和之间使用collect()。toMap:Collectors.toMap(Function.identity(), this::getDepartment, (a,b) -> a)| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |