Jul*_*lez 5 java hashmap set java-8 java-stream
如何使用流从字符串集合中创建字符串的 HashMap 和字符串列表?
Set<String> mySet;
Map<String, List<String>> = mySet.stream().map(string -> {
// string will be my key
// I have here codes that return List<String>
// what to return here?
}).collect(Collectors.toMap(.....)); // what codes needed here?
Run Code Online (Sandbox Code Playgroud)
谢谢你。
你不需要这map()一步。List<String>从 a产生 a 的逻辑String应该传递给Collectors.toMap():
Map<String, List<String>> map =
mySet.stream()
.collect(Collectors.toMap(Function.identity(),
string -> {
// put logic that returns List<String> here
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5728 次 |
| 最近记录: |