流到新集合的集合 [英] Collection to stream to a new collection

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

问题描述

我正在寻找最轻松的方式来过滤集合.我在想像

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)

    这篇关于流到新集合的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆