我一直在使用Java 8 lambdas来轻松地过滤集合。但是我没有找到一种简洁的方法来检索结果,将其作为同一语句中的新列表。以下是我目前为止最简洁的方法:
List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
List<Long> targetLongList = new ArrayList<>();
sourceLongList.stream().filter(l -> l > 100).forEach(targetLongList::add);
网上的示例没有回答我的问题,因为它们没有生成新的结果列表就停止了。一定有更简洁的方法。我本来希望,Stream类有toList(), toSet(),…
是否有一种方法,变量targetLongList可以直接由第三行分配?
在一个可变列表中收集:
targetList = sourceList.stream()
.filter(i -> i > 100) //apply filter
.collect(Collectors.toList());
在一个不可变列表中收集:
targetList = sourceList.stream()
.filter(i -> i > 100) //apply filter
.collect(Collectors.toUnmodifiableList());
从JavaDoc中collect的解释:
Performs a mutable reduction operation on the elements of this stream
using a Collector. A Collector encapsulates the functions used as
arguments to collect(Supplier, BiConsumer, BiConsumer), allowing for
reuse of collection strategies and composition of collect operations
such as multiple-level grouping or partitioning. If the stream is
parallel, and the Collector is concurrent, and either the stream is
unordered or the collector is unordered, then a concurrent reduction
will be performed (see Collector for details on concurrent reduction.)
This is a terminal operation.
When executed in parallel, multiple intermediate results may be
instantiated, populated, and merged so as to maintain isolation of
mutable data structures. Therefore, even when executed in parallel
with non-thread-safe data structures (such as ArrayList), no
additional synchronization is needed for a parallel reduction.
我喜欢使用util方法,当我需要时,它会为ArrayList返回一个收集器。
我认为使用collections . tocollection (ArrayList::new)的解决方案对于这样一个常见的操作来说有点太吵了。
例子:
ArrayList<Long> result = sourceLongList.stream()
.filter(l -> l > 100)
.collect(toArrayList());
public static <T> Collector<T, ?, ArrayList<T>> toArrayList() {
return Collectors.toCollection(ArrayList::new);
}
有了这个答案,我还想演示创建和使用自定义收集器是多么简单,这通常是非常有用的。