【问题标题】:Java – Combine Multiple CollectionsJava – 组合多个集合
【发布时间】:2023-04-02 19:30:01
【问题描述】:

我有这段代码,我想使用更多的 Java 8 方法进行重构,但我知道有几个选项可以做到这一点: concat() Java 8 Stream API , flatMap() Java 8 Stream API ,使用 Guava,使用 Apache Commons Collections,CompletableFuture.... 我想知道是否有这样做的最佳做法

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());  
}

【问题讨论】:

    标签: java java-8 functional-programming java-stream


    【解决方案1】:

    这样的?

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

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2017-04-20
      相关资源
      最近更新 更多