3

I have this piece of code, that I want to refactor using more the Java 8 approach, but I know that there are several options to do this: concat() Java 8 Stream API , flatMap() Java 8 Stream API , Using Guava, Using Apache Commons Collections, CompletableFuture.... I would like to know if there is a best practice to do this

List<User> users = new ArrayList<User>(); for (Restaurant restaurant : restaurants) { users.addAll(userService.getClients(restaurant.getId()) .parallelStream() .filter(us -> !alreadyNotifiedUserIds.contains(us.getId()))) .collect(Collectors.toList()); } 

1 Answer 1

5

Something like this?

List<User> users = restaurants.parallelStream() .flatMap(r -> userService.getClients(r.getId()).stream()) .filter(us -> !alreadyNotifiedUserIds.contains(us.getId())) .collect(Collectors.toList()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.