3

When using external iteration over an java collectios we use keyword "continue" for move to the next value in a loop. What about in the case, when we use java stream API? Example:

List<User> users;

for(Item item : item){
    users.stream()
        .filter(user -> user.getName().equals("Tom"))
        .findFirst()
        .ifPresent(move to the next value in a outer loop(items) ???? like **continue**;);

    more code here...
}

Without stream:

 for(Item item : item){
    for(User user : users){
     if(user.getName("Tom") exist = true;
    }
  if (exist) continue;

    more code here...
}
5
  • 2
    What you want to do exactly? What you want to do after you find any match ? Commented Jun 5, 2018 at 13:33
  • findFirst() returns just a single element (if present). If you want to iterate over all the elements, use forEach(). Commented Jun 5, 2018 at 13:33
  • I want to skip itteration, if user.getName().equals("Tom"), only one itteration, and move to next item Commented Jun 5, 2018 at 13:37
  • so you want to get the next entry after Tom? Commented Jun 5, 2018 at 13:42
  • still unclear... show how you would do it without streams. Commented Jun 5, 2018 at 13:47

3 Answers 3

2

Java stream api is used only to work on that particular stream of elements. You can never use it for control statements like ‘break’ or ‘continue’ to control external loops (like here you want to control ‘Item’ loop).

Sign up to request clarification or add additional context in comments.

Comments

2

Maybe you just want:

for (Item item : item){
    if (users.stream().anyMatch(u -> u.getName().equals("Tom"))) {
        continue;
    }

    // more code here...
}

3 Comments

I'd firstly evaluate the condition and then enter the loop.
@Nikolas except "more code here" might add Tom to users.
@Boheamian: A very good point! :)) +1.. It depends that "more code here" really does.
2

The Stream-API doesn't provide the iteration mechanism like the Iterator does, for this you have to use the Iterator<User>. However, it's good to filter the correct values first.

List<User> filteredUsers = users.stream()
    .filter(user -> user.getName().equals("Tom"))
    .collect(Collectors.toList());

Now get an Iterator<User> and iterate through.

Iterator<User> iterator = filteredUsers.iterator();
while (iterator.hasNext()) {
    User user = iterator.next()
    // Take a control over the further iteration here...
}

Edit:

According to your edit, you want to find out if any User has a name Tom, then perform the iteration over the items. Use the anyMatch(Predicate<? super T> predicate) method which returns true if any element of the stream matches the predicate. It accepts the same predicate as the filter(Predicate<? super T> predicate) does.

if (users.stream().anyMatch(user -> user.getName().equals("Tom"))) {
    for (Item item : item) {
        // ...
    }
}

Edit2: As @Bohemian has correctly noted, consider checking the presence of the element with each iteration since there may be an element with "Tom" added and the condition no longer remains false.

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.