8

In the below example , someObjects is a set. I am trying to return true if a condition matches within the loop , however this doesn't seem to compile. However when I just add "return" it works fine.What is the issue that I need to fix?

public boolean find(){

    someObjects.forEach(obj -> {
       if (some_condition_met) {
          return true;
       }
    });

    return false;
}

Compilation Errors

The method forEach(Consumer) in the type Iterable is not applicable for the arguments (( obj) -> {})

11
  • Where do you add "return" to make it work? Commented Oct 29, 2017 at 21:54
  • 1
    What are the compiler errors? Add the output to the question. Commented Oct 29, 2017 at 21:54
  • 2
    It is impossible to return from the outer method inside the body of a lambda. Use a for loop instead Commented Oct 29, 2017 at 21:56
  • Thanks @cppbeginner. So when I add a return (without return value) , it just exits the loop? Commented Oct 29, 2017 at 21:57
  • 3
    I love this answer. stackoverflow.com/a/20177092/6253321 Commented Oct 29, 2017 at 22:05

3 Answers 3

17

I guess you want to do this:

public boolean find(){
    return someObjects.stream().anyMatch(o -> your_condition);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Probably not. There's an overhead in building a stream. Much better to do it the old-fashioned (pre Java 8) way.
@DawoodibnKareem i assume OP wants something in functional style. judging from his scores, he should know how to do it in the old-fashioned way.
Maybe, but this is clearly a case where the "functional style" is the wrong solution.
@DidierL I entirely agree with you. real life software practice puts micro performance like this to the very back of the priority queue. there are lots of arguments to object that claim.
@DawoodibnKareem Considering everything implemented with streams can be rewritten without, by your logic you should never use streams then.
|
11

The forEach method in a Collection expects a Consumer which means a function that takes a value, but doesn't return anything. That's why you can't use return true; but a return; works fine.

I you want to break out of the loop when your condition is met, it's better to use a simple for(...) loop. I assumed that the type of obj is Object:

for (Object obj : someObjects) {
  if (some_condition_met) {
    return true;
  }
}

return false;

4 Comments

Thanks , how does return work when the return is void , I assume? Do return just exit the loop?
No. In your example a return; will simply exit the lambda and proceed to the next element in the collection. The loop will not be ended.
Thank you! So I guess it is same as continue.
Yes, it's effectively the same as a continue in a "standard" loop.
4

forEach accepts a Consumer therefore you cannot pass in a behaviour that does not return void. you need to do something like:

return someObjects.stream().anyMatch(e -> condition);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.