Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 462
GedankenNebel Avatar asked Dec 15 '22 23:12

GedankenNebel


1 Answers

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());
}
like image 108
Brett Kail Avatar answered Dec 28 '22 01:12

Brett Kail