Java 流到新集合的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21522341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Collection to stream to a new collection
提问by xenoterracide
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.
但我不确定如何最好地从过滤器转到返回或填充另一个集合。大多数示例似乎都类似于“您可以在这里打印”。可能有我缺少的构造函数或输出方法。
采纳答案by Holger
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.
大多数示例避免将结果存储到Collection
. 这不是推荐的编程方式。您已经有了一个Collection
,提供源数据和集合的那个本身是没有用的。您想对其执行某些操作,因此理想的情况是使用流执行操作并跳过将数据存储在中间Collection
. 这是大多数例子试图表明的。
Of course, there are a lot of existing APIs working with Collection
s and there always will be. So the Stream
API offers different ways to handle the demand for a Collection
.
当然,有很多现有的 API 与Collection
s一起工作,而且总会有的。因此,Stream
API 提供了不同的方法来处理对Collection
.
Get an arbitrary
List
implementation holding the result:List<T> results = l.stream().filter(…).collect(Collectors.toList());
Get an arbitrary
Set
implementation holding the result:Set<T> results = l.stream().filter(…).collect(Collectors.toSet());
Get a specific
Collection
:ArrayList<T> results = l.stream().filter(…).collect(Collectors.toCollection(ArrayList::new));
Add to an existing
Collection
: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);
(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 thetoList()
collectors will still return anArrayList
)
获取
List
保存结果的任意实现:List<T> results = l.stream().filter(…).collect(Collectors.toList());
获取
Set
保存结果的任意实现:Set<T> results = l.stream().filter(…).collect(Collectors.toSet());
得到一个具体的
Collection
:ArrayList<T> results = l.stream().filter(…).collect(Collectors.toCollection(ArrayList::new));
添加到现有的
Collection
:l.stream().filter(…).forEach(existing::add);
创建一个数组:
String[] array=l.stream().filter(…).toArray(String[]::new);
使用数组创建具有特定特定行为(可变、固定大小)的列表:
List<String> al=Arrays.asList(l.stream().filter(…).toArray(String[]::new));
允许具有并行能力的流添加到临时本地列表并在之后加入它们:
List<T> results = l.stream().filter(…).collect(ArrayList::new, List::add, List::addAll);
(注意:这与
Collectors.toList()
当前的实现方式密切相关,但这是一个实现细节,即不能保证toList()
收集器的未来实现仍会返回ArrayList
)
回答by nobeh
As an example that is more in line with Java 8 style of functional programming:
作为一个更符合 Java 8 函数式编程风格的例子:
Collection<String> a = Collections.emptyList();
List<String> result = a.stream().
filter(s -> s.length() > 0).
collect(Collectors.toList());
回答by Samy Dindane
An example from java.util.stream
's documentation:
java.util.stream
的文档中的一个示例:
List<String>results =
stream.filter(s -> pattern.matcher(s).matches())
.collect(Collectors.toList());
Collectors
has a toCollection()
method, I'd suggest looking this way.
Collectors
有一个toCollection()
方法,我建议这样看。