使用流有条件地填充地图 - Java 8 [英] Populate a map conditionally using streams - Java 8

查看:157
本文介绍了使用流有条件地填充地图 - Java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此(简化)代码翻译为使用Java-8流:

I'm trying to translate this (simplified) code to use Java-8 streams:

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();

这是我尝试过的:

files = reader.lines().parallel().collect((content != null)?
                (Collectors.toConcurrentMap(line->"notnull"+line, line->line+"notnull")) :                                              
                (Collectors.toConcurrentMap(line->line+"null", line->line+"null")));

但上面给出了所有 line-> line +的循环推断消息。 on intelliJ。什么是循环推理?这个逻辑中是否有错误?

But the above gives a "cyclic inference" message for all the line->line+"..." on intelliJ. What is cyclic inference? Is there an error in this logic?

我注意到SO上有类似的问题。但是他们建议使用接口(Map)而不是它的实现。但文件这里是d eclared为地图

I noted some similar issues on SO. But they suggest to use interface(Map) instead of its implementations. But files here is declared as a Map.

更新:添加更多上下文,内容是一个包含目录名称的String。 files 是一个包含多个文件路径的地图。需要进入文件映射的文件路径取决于 content 是否填充了directory-name。

Update: Adding more context, content is a String that holds the name of a directory. files is a map that holds multiple file paths. What file paths need to go into the files map depends on content directory-name is populated or not.

推荐答案

另一种解决方法是为收集器引入中间变量:

Another way to fix this is to introduce the intermediate variable for collector:

Collector<String, ?, ConcurrentMap<String, String>> collector = (content != null) ?
        (Collectors.toConcurrentMap(line->"notnull"+line, line->line+"notnull")) :
        (Collectors.toConcurrentMap(line->line+"null", line->line+"null"));
Map<String, String> files = reader.lines().parallel().collect(collector);       

此解决方案(与@JanXMarek提供的解决方案不同)不分配中间数组,也不检查<每个输入行的code>内容。

This solution (unlike one presented by @JanXMarek) does not allocate intermediate arrays and does not check the content for every input line.

循环推断是类型推断过程中的情况,何时确定类型在内部子表达式中,必须确定外部子表达式的类型,但在不知道内部子表达式的类型的情况下无法确定。 Java-8中的类型推断可以推断出 Stream< String> .collect(Collectors.toConcurrentMap(line-> line +null,line-> line +null))收集器的类型是收集器< String,?,ConcurrentMap< String,String>> 。通常,当子表达式类型(这里我们说的是 toConcurrentMap(...)子表达式)无法显式确定时,如果外部上下文是外部上下文,则可以使用外部上下文来减少它方法调用,强制转换或赋值。然而,外部上下文是?:运算符,它有自己的复杂类型推断规则,所以这变得太多了,你应该帮助类型推断系统在某处指定显式类型。

The cyclic inference is the situation in the type inference procedure when to determine the type of inner subexpression, the type of outer subexpression must be determined, but it cannot be determined without knowing the type of inner subexpression. Type inference in Java-8 can infer that in case of Stream<String>.collect(Collectors.toConcurrentMap(line->line+"null", line->line+"null")) the type of Collector is Collector<String, ?, ConcurrentMap<String, String>>. Normally when subexpression type (here we're speaking about toConcurrentMap(...) subexpression) cannot be explicitly determined, it can be reduced using the outer context if the outer context is method invocation, cast or assignment. Here however the outer context is ?: operator which has its own complex type inference rules, so this becomes too much and you should help the type inference system specifying the explicit type somewhere.

这篇关于使用流有条件地填充地图 - Java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆