【问题标题】:Collect Java8 IntStream into Deque interface将 Java8 IntStream 收集到 Deque 接口中
【发布时间】:2018-10-28 21:55:21
【问题描述】:

如何将Java8 IntStream 收集到Deque 接口中? 我可以像这样使用 List 执行这种操作:

List<Integer> integerList = IntStream.of(1, 2, 3)
                                     .boxed()
                                     .collect(Collectors.toList());

【问题讨论】:

    标签: collections java-8


    【解决方案1】:

    使用Collectors.toCollection指定你想要的集合,例如:

    .collect(Collectors.toCollection(ArrayDeque::new));
    

    Deque 接口的任何其他实现。

    【讨论】:

      【解决方案2】:

      您不能收集到接口,而是通过Collectors.toCollection 收集到它的实现(只要它是Collection

       Deque<Integer> d = IntStream.of(1, 2)
                  .boxed()
                  .collect(Collectors.toCollection(ArrayDeque::new));
      

      【讨论】:

      • 快如闪电^_^
      猜你喜欢
      • 2018-10-14
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多