Java 8 中带有流到 HashMap 的字符串集

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)

谢谢你。

Era*_*ran 5

你不需要这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)