【问题标题】:Merge lists with stream API使用流 API 合并列表
【发布时间】:2014-05-31 12:41:05
【问题描述】:

我有以下情况

Map<Key, ListContainer> map; 

public class ListContainer {
  List<AClass> lst;
}

我必须合并来自Map 映射的ListContainer 对象的所有列表lst

public static void main(String[] args) {
   List<AClass> alltheObjectsAClass = map.values().stream(). // continue....    
}

知道如何使用 Java 8 流 API 吗?

【问题讨论】:

  • 你能举个例子,你对合并的意思是什么?假设你的地图是{a: [1,2], b[3,4]},你是想将它们链接起来,比如[1,2,3,4],还是制作一个列表列表,[[1,2],[3,4]],或者压缩它们[[1,3],[2,4]]?另外,您知道地图没有顺序,是吗?
  • @tobias_k 我希望结果是[1,2,3,4],未排序!
  • 如果ListContainer 只包含List&lt;T&gt;,那么您可以将Map&lt;Key, ListContainer&gt; 替换为Map&lt;Key, List&lt;T&gt;&gt;

标签: java list java-8 java-stream


【解决方案1】:

要将多个列表或其他类型的集合合并为一个,您可以使用以下方法:

Stream.of(list1.stream(), list2.stream(), someSet.stream(), otherCollection.stream())
    .flatMap(Function.identity())
    .collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    上面已经回答了,但这是您可以采取的另一种方法。我找不到我改编的原始帖子,但为了您的问题,这里是代码。如上所述,flatMap() 函数是您希望在 Java 8 中使用的函数。您可以将其放入实用程序类中,然后调用“RandomUtils.combine(list1, list2, ...);”你会得到一个包含所有值的列表。请注意通配符 - 如果您想要一个不太通用的方法,您可以更改它。您还可以为 Set 修改它 - 在 Set 上使用 flatMap() 时必须小心,以避免由于 Set 接口的性质而从 equals/hashCode 方法中丢失数据。

    编辑 - 如果您对 Set 接口使用这样的通用方法,并且碰巧使用 Lombok,请确保您了解 Lombok 如何处理 equals/hashCode 生成。

      /**
        * Combines multiple lists into a single list containing all elements of
        * every list.
        * 
        * @param <T> - The type of the lists.
        * @param lists - The group of List implementations to combine
        * @return a single List<?> containing all elements of the passed in lists.
        */
       public static <T> List<?> combine(final List<?>... lists) {
          return Stream.of(lists).flatMap(List::stream).collect(Collectors.toList());
       }
    

    【讨论】:

      【解决方案3】:

      在 Java 8 中,我们可以使用流 List1.stream().collect(Collectors.toList()).addAll(List2);另一种选择 List1.addAll(List2)

      【讨论】:

        【解决方案4】:

        我认为flatMap() 是您要找的。​​p>

        例如:

         List<AClass> allTheObjects = map.values()
                 .stream()
                 .flatMap(listContainer -> listContainer.lst.stream())
                 .collect(Collectors.toList());
        

        【讨论】:

        • ?为什么删除了解释为什么 .flatMap(Collection::stream) 在这里不可能的 cmets?
        • @Puce 很好的问题,但它是有可能的:如果ListContainer 被封装(即有一个lst 的吸气剂),您可以将.flatMap(-&gt;) 分解为.map(ListContainer::getLst) .flatMap(Collection::stream)
        • @TWiStErRob 是的,这就是我在原始评论中所写的。为什么会被删除?
        • @Puce 在你的回答中说明为什么.flatMap(Collection::stream) 不可能,我认为会更好。应该更永久。
        • @Alex 是的,这是一种方式。 .map(listContainer -&gt; listContainer.lst).filter(Objects::nonNull).flatMap(Collection::stream)
        【解决方案5】:

        替代方案:Stream.concat()

        Stream.concat(map.values().stream(), listContainer.lst.stream())
                                     .collect(Collectors.toList()
        

        【讨论】:

        • 加 1 即使它不回答问题,它也会回答问题的标题
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多