4

Trying to filter a collection using a stream, and trying to pass the following lambda to filter() a Set, which gives the arcane error in the title:

unmatchedIncomingFields.stream().filter(s-> s.matches(fieldMatchPattern))

Meanwhile, creating a Predicate object works:

unmatchedIncomingFields.stream().filter(new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.matches(fieldMatchPattern);
    }
});

According to the JLS, a lambda body is "value-compatible" if every control path returns a value. matches() always gets called and always returns a boolean, so I don't understand what the problem is.

I've also tried all kinds of variations of the same lambda- with and without parentheses and argument types and using expression and block-with-return bodies.

4
  • what's the type of unmatchedIncomingFields ? Commented Apr 25, 2016 at 7:21
  • Can you include a small reproductible example, and also the compiler version you are using?
    – Alexis C.
    Commented Apr 25, 2016 at 7:23
  • @AlexisC. The compiler is 1.8.0_77. I'm working on getting a demonstration. Commented Apr 25, 2016 at 7:56
  • what's the generic type of the elements in the Set, is it Set<String> or just Set? Commented Apr 25, 2016 at 8:11

1 Answer 1

9

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.

2
  • Correct, similar thing has just happened to me. Thank you!
    – Marcin
    Commented Sep 14, 2016 at 9:51
  • 1
    This looks like an actual bug in Intellij, as the same thing just happened to me. Have we filed an issue yet? Commented Aug 3, 2017 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.