-2

I attempted to make a multiple line Predicate by first adding curly brackets but Eclipse complains about the following code:

Stream<String> stringStream = temp.flatMap(x -> Arrays.stream(x));
Stream<String> stream = stringStream.filter(x -> {"a".equals(x.toString());});

It says after placing a red line under filter: "The method filter(Predicate<? super String>) in the type Stream<String> is not applicable for the arguments ((<no type> x) -> {})"

Is this because multiline predicates are not allowed or am I making some other mistake?

Naman
  • 27,789
  • 26
  • 218
  • 353
releseabe
  • 323
  • 3
  • 13
  • 1
    Aside: `stringStream.filter("a"::equals)` would be sufficiently equal for the code in question. – Naman Dec 19 '19 at 17:52

3 Answers3

2

If you use brackets, you need to include the return keyword. If it is a single expression you can omit both the brackets and the return keyword.

Naman
  • 27,789
  • 26
  • 218
  • 353
SephB
  • 617
  • 3
  • 6
0

The {} make a small block of code. So you need to return true or false int the predicate.

Stream<String> stream = stringStream.filter(x -> {return "a".equals(x.toString());});
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Either do this,

Stream<String> stream = stringStream.filter(x -> "a".equals(x.toString()));

or add the return

Stream<String> stream = stringStream.filter(x -> {return "a".equals(x.toString());});
Adwait Kumar
  • 1,552
  • 10
  • 25