Java – Combine Multiple Collections

carles xuriguera :

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());  
}
David Pérez Cabrera :

Something like this?

List<User> users = restaurants.parallelStream()
    .flatMap(r -> userService.getClients(r.getId()).stream())
    .filter(us -> !alreadyNotifiedUserIds.contains(us.getId()))
    .collect(Collectors.toList());  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=89875&siteId=1