想象一下,我有一个像这样的文件:
1, Apple
1, Pear
1, Orange
2, Apple
3, Melon
3, Orange
Run Code Online (Sandbox Code Playgroud)
我想把它解析成一个列表,每个条目都是一个地图,或者我猜它可能是我自己的对象,但我认为一张地图最好,因为它是一把钥匙.值.
我在努力:
private List<Map<String, String>> readRecords(String path) {
return Files.lines(Paths.get(path))
.map(line -> Arrays.asList(line.split(SEPARATOR)))
.map(snippets -> new HashMap<Integer, String>().put(Integer.parseInt(snippets.get(0)), snippets.get(1)))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
但这给了我一个关于无法在List<String>和之间转换的错误List<Map<String, String>>
或者也许有更好的方法来做到这一点?