尝试使用流过滤集合,并尝试将以下lambda传递给filter()一个Set,它在标题中给出了一个神秘的错误:
unmatchedIncomingFields.stream().filter(s-> s.matches(fieldMatchPattern))
同时,创建Predicate对象有效:
unmatchedIncomingFields.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.matches(fieldMatchPattern);
}
});
根据JLS,一个lambda身体是&#34;价值兼容&#34;如果每个控制路径都返回一个值。 matches()总是被调用并且总是返回一个布尔值,所以我不明白这个问题是什么。
我还尝试了相同lambda的各种变体 - 有和没有括号和参数类型,并使用表达式和块返回体。
答案 0 :(得分:6)
The issue looks to be incorrect, or at least somewhat misleading, error highlighting within IntelliJ, confusing where the actual error was.
The filter occurs within another lambda for a map() operation which I had not specified a return for yet, and for some reason, IntelliJ highlights the inner lambda for the filter, making it look like it is the one with the error.