GedankenNebel GedankenNebel - 4 months ago 18
Java Question

Break/Stop forEachRemaning using Java8

Consider following snippet:

private List<User> getUsers() throws TasteException {
final int MAX_USERS = 100;
List<User> userList = new ArrayList<>(MAX_USERS);
dataModel.getUserIDs().forEachRemaining(userId -> {
if (userList.size() == 100) {
// stop
}
userList.add(new User(userId));
});
return userList;
}


break
or
return
is not working here. What can I do?

Answer

The only way to stop the iteration early is to throw an exception. Using exceptions for control flow isn't advised, so I would use Stream.limit, .map, and .collect:

private List<User> getUsers() throws TasteException {
  final int MAX_USERS = 100;
  return dataModel.getUserIDs()
                  .stream()
                  .limit(MAX_USERS)
                  .map(userId -> new User(userId))
                  .collect(Collectors.toList());
}

If getUserIDs can't be changed to return Collection you can convert to a Spliterator first:

private List<User> getUsers() throws TasteException {
  final int MAX_USERS = 10;
  return StreamSupport.stream(Spliterators.spliteratorUnknownSize(dataModel.getUserIDs(), 0), false)
                      .limit(MAX_USERS)
                      .map(userId -> new User(userId))
                      .collect(Collectors.toList());
}
Comments