集合以流式传输到新集合

Collection to stream to a new collection(集合以流式传输到新集合)

本文介绍了集合以流式传输到新集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最轻松的方法来过滤集合.我在想类似的东西

I'm looking for the most pain free way to filter a collection. I'm thinking something like

Collection<?> foo = existingCollection.stream().filter( ... ). ...

但我不确定如何最好地从过滤器返回或填充另一个集合.大多数示例似乎就像在这里您可以打印".可能有我缺少的构造函数或输出方法.

But I'm not sure how is best to go from the filter, to returning or populating another collection. Most examples seem to be like "and here you can print". Possible there's a constructor, or output method that I'm missing.

推荐答案

大多数示例避免将结果存储到 Collection 中是有原因的.这不是推荐的编程方式.您已经有一个 Collection,提供源数据和集合的那个本身没有用.您想对其执行某些操作,因此理想的情况是使用流执行操作并跳过将数据存储在中间 Collection 中.这是大多数例子试图暗示的.

There’s a reason why most examples avoid storing the result into a Collection. It’s not the recommended way of programming. You already have a Collection, the one providing the source data and collections are of no use on its own. You want to perform certain operations on it so the ideal case is to perform the operation using the stream and skip storing the data in an intermediate Collection. This is what most examples try to suggest.

当然,有很多现有的 API 与 Collection 一起使用,而且总会有.因此,Stream API 提供了不同的方式来处理对 Collection 的需求.

Of course, there are a lot of existing APIs working with Collections and there always will be. So the Stream API offers different ways to handle the demand for a Collection.

  • 获取包含所有元素的不可修改的 List 实现(JDK 16):

List<T> results = l.stream().filter(…).toList();

  • 获取一个任意的List实现来保存结果:

    List<T> results = l.stream().filter(…).collect(Collectors.toList());
    

  • 获取 不可修改的 List 禁止 nullList.of(...) (JDK 10):

    List<T> results = l.stream().filter(…).collect(Collectors.toUnmodifiableList());
    

  • 获取一个任意的Set实现来保存结果:

    Set<T> results = l.stream().filter(…).collect(Collectors.toSet());
    

  • 获取特定的Collection:

    ArrayList<T> results =
      l.stream().filter(…).collect(Collectors.toCollection(ArrayList::new));
    

  • 添加到现有的集合:

    l.stream().filter(…).forEach(existing::add);
    

  • 创建一个数组:

  • Create an array:

    String[] array=l.stream().filter(…).toArray(String[]::new);
    

  • 使用数组创建具有特定特定行为(可变、固定大小)的列表:

  • Use the array to create a list with a specific specific behavior (mutable, fixed size):

    List<String> al=Arrays.asList(l.stream().filter(…).toArray(String[]::new));
    

  • 允许并行流添加到临时本地列表并在之后加入它们:

  • Allow a parallel capable stream to add to temporary local lists and join them afterward:

    List<T> results
      = l.stream().filter(…).collect(ArrayList::new, List::add, List::addAll);
    

    (注意:这与当前如何实现 Collectors.toList() 密切相关,但这是一个实现细节,即不能保证 toList() 的未来实现 收集器仍然会返回一个 ArrayList)

    (Note: this is closely related to how Collectors.toList() is currently implemented, but that’s an implementation detail, i.e. there is no guarantee that future implementations of the toList() collectors will still return an ArrayList)

    这篇关于集合以流式传输到新集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本文标题为:集合以流式传输到新集合

    基础教程推荐