我正在尝试将此(简化)代码翻译为使用Java-8流:
Map<String, String> files = new ConcurrentHashMap<String, String>();
while(((line = reader.readLine()) != null) {
if(content != null)
files.put("not null"+line, "not null"+line);
else
files.put("its null"+line, "its null"+line);
}
reader.close();
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
files = reader.lines().parallel().collect((content != null)?
(Collectors.toConcurrentMap(line->"notnull"+line, line->line+"notnull")) :
(Collectors.toConcurrentMap(line->line+"null", line->line+"null")));
Run Code Online (Sandbox Code Playgroud)
但是上面给出了所有line->line+"..."on intelliJ 的"循环推理"消息.什么是循环推理?这个逻辑中有错误吗?
我在SO上注意到了一些类似的问题.但他们建议使用接口(Map)而不是其实现.但files这里被宣布为Map.
更新:添加更多上下文,content是一个包含目录名称的String.files是一个包含多个文件路径的地图.需要进入files映射的文件路径取决于content是否填充了目录名.
在下面的代码中Intellij说"循环推理"
List<String> rows = new ArrayList<>();
rows.add("12345");
rows.add("123");
rows.add("123456");
rows = rows.stream().filter(e -> e.length() > 4).collect(Collectors::toList);
rows.stream().forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
必须有一些Collectors::toList我无法理解的问题.