我试图转换以下文本输入文件:
A=groupA1
A=groupA2
A=groupA3
B=groupB1
B=groupB2
Run Code Online (Sandbox Code Playgroud)
成Map<String, List<String>>通过划分在每一行"="
到目前为止,我想要获得这种输出:
KEY: A
VALUE: A=groupA1
VALUE: A=groupA2
VALUE: A=groupA3
KEY: B
VALUE: B=groupB1
VALUE: B=groupB2
Run Code Online (Sandbox Code Playgroud)
使用这样的代码:
File reqFile = new File("test.config");
try (Stream<String> stream = Files.lines(reqFile.toPath())) {
Map<String, List<String>> conf = stream.collect(Collectors.groupingBy(s -> s.split("=")[0]));
for (Map.Entry<String, List<String>> entry: conf.entrySet()) {
System.out.println("KEY: " + entry.getKey());
for (String value : entry.getValue()) {
System.out.println("VALUE: " + value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如何调整上面的lambda得到这样的东西:
KEY: A
VALUE: groupA1
VALUE: …Run Code Online (Sandbox Code Playgroud)