public final class Collectors
extends Object
java.lang.Object | |
↳ | java.util.stream.Collectors |
执行各种有用的缩减操作的实现 Collector
,例如将元素累加到集合中,根据各种标准对元素进行汇总等。
以下是使用预定义收集器执行常见可变减少任务的示例:
// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
// Accumulate names into a TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// Compute sum of salaries of employee
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Compute sum of salaries by department
Map<Department, Integer> totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
Public methods |
|
---|---|
static <T> Collector<T, ?, Double> |
averagingDouble(ToDoubleFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, Double> |
averagingInt(ToIntFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, Double> |
averagingLong(ToLongFunction<? super T> mapper) 返回一个 |
static <T, A, R, RR> Collector<T, A, RR> |
collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)
|
static <T> Collector<T, ?, Long> |
counting() 返回 |
static <T, K> Collector<T, ?, Map<K, List<T>>> |
groupingBy(Function<? super T, ? extends K> classifier) 返回一个 |
static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> |
groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream) 返回 |
static <T, K, A, D> Collector<T, ?, Map<K, D>> |
groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream) 返回 |
static <T, K, A, D> Collector<T, ?, ConcurrentMap<K, D>> |
groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream) 返回并发 |
static <T, K> Collector<T, ?, ConcurrentMap<K, List<T>>> |
groupingByConcurrent(Function<? super T, ? extends K> classifier) 返回一个并发的 |
static <T, K, A, D, M extends ConcurrentMap<K, D>> Collector<T, ?, M> |
groupingByConcurrent(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream) 返回并发 |
static Collector<CharSequence, ?, String> |
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 返回一个 |
static Collector<CharSequence, ?, String> |
joining(CharSequence delimiter) 返回 |
static Collector<CharSequence, ?, String> |
joining() 返回一个 |
static <T, U, A, R> Collector<T, ?, R> |
mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream) 适应一个 |
static <T> Collector<T, ?, Optional<T>> |
maxBy(Comparator<? super T> comparator) 返回 |
static <T> Collector<T, ?, Optional<T>> |
minBy(Comparator<? super T> comparator) 返回一个 |
static <T> Collector<T, ?, Map<Boolean, List<T>>> |
partitioningBy(Predicate<? super T> predicate) 返回 |
static <T, D, A> Collector<T, ?, Map<Boolean, D>> |
partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream) 返回根据 |
static <T> Collector<T, ?, Optional<T>> |
reducing(BinaryOperator<T> op) 返回 |
static <T, U> Collector<T, ?, U> |
reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op) 返回一个 |
static <T> Collector<T, ?, T> |
reducing(T identity, BinaryOperator<T> op) 返回一个 |
static <T> Collector<T, ?, DoubleSummaryStatistics> |
summarizingDouble(ToDoubleFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, IntSummaryStatistics> |
summarizingInt(ToIntFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, LongSummaryStatistics> |
summarizingLong(ToLongFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, Double> |
summingDouble(ToDoubleFunction<? super T> mapper) 返回一个 |
static <T> Collector<T, ?, Integer> |
summingInt(ToIntFunction<? super T> mapper) 返回 |
static <T> Collector<T, ?, Long> |
summingLong(ToLongFunction<? super T> mapper) 返回一个 |
static <T, C extends Collection<T>> Collector<T, ?, C> |
toCollection(Supplier<C> collectionFactory) 返回一个 |
static <T, K, U, M extends ConcurrentMap<K, U>> Collector<T, ?, M> |
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) 返回并发的 |
static <T, K, U> Collector<T, ?, ConcurrentMap<K, U>> |
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) 返回并发的 |
static <T, K, U> Collector<T, ?, ConcurrentMap<K, U>> |
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) 返回并发的 |
static <T> Collector<T, ?, List<T>> |
toList() 返回一个 |
static <T, K, U> Collector<T, ?, Map<K, U>> |
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) 返回一个 |
static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> |
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) 返回一个 |
static <T, K, U> Collector<T, ?, Map<K, U>> |
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) 返回一个 |
static <T> Collector<T, ?, Set<T>> |
toSet() 返回一个 |
Inherited methods |
|
---|---|
From class java.lang.Object
|
Collector<T, ?, Double> averagingDouble (ToDoubleFunction<? super T> mapper)
返回Collector
,该值产生应用于输入元素的双值函数的算术平均值。 如果没有元素存在,结果为0。
返回的平均值可能会因记录值的顺序而有所不同,这是由于累加的舍入误差以及不同数值的值。 按增加绝对值排序的值倾向于产生更准确的结果。 如果任何记录的值是NaN
或总和在任何一点NaN
那么平均值将是NaN
。
double
format can represent all consecutive integers in the range -253 to 253. If the pipeline has more than 253 values, the divisor in the average computation will saturate at 253, leading to additional numerical errors.Parameters | |
---|---|
mapper |
ToDoubleFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
Collector<T, ?, Double> averagingInt (ToIntFunction<? super T> mapper)
返回一个Collector
,它产生应用于输入元素的整数值函数的算术平均值。 如果没有元素存在,结果为0。
Parameters | |
---|---|
mapper |
ToIntFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
Collector<T, ?, Double> averagingLong (ToLongFunction<? super T> mapper)
返回一个Collector
,它产生应用于输入元素的长值函数的算术平均值。 如果没有元素存在,结果为0。
Parameters | |
---|---|
mapper |
ToLongFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
Collector<T, A, RR> collectingAndThen (Collector<T, A, R> downstream, Function<R, RR> finisher)
Collector
以执行额外的修整转换。 例如,可以调整toList()
收集器以始终生成一个不可变列表:
List<String> people
= people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
Parameters | |
---|---|
downstream |
Collector : a collector |
finisher |
Function : a function to be applied to the final result of the downstream collector |
Returns | |
---|---|
Collector<T, A, RR> |
a collector which performs the action of the downstream collector, followed by an additional finishing step |
Collector<T, ?, Long> counting ()
返回Collector
类型的接受元件T
计数输入元件的数量。 如果没有元素存在,结果为0。
reducing(0L, e -> 1L, Long::sum)
Returns | |
---|---|
Collector<T, ?, Long> |
a Collector that counts the input elements |
Collector<T, ?, Map<K, List<T>>> groupingBy (Function<? super T, ? extends K> classifier)
返回 Collector
“由基团”上的类型的输入元件操作实现 T
,根据分类功能分组元素,并且在返回的结果 Map
。
分类功能将元素映射到某些关键类型K
。 收集器产生一个Map<K, List<T>>
其键值是将分类函数应用于输入元素所得到的值,其对应的值为List
其中包含映射到分类函数下相关键值的输入元素。
对于返回的 Map
或 List
对象的类型,可变性,可串行性或线程安全性没有保证。
groupingBy(classifier, toList());
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements appear in the resulting Map
collector is not required, using groupingByConcurrent(Function)
may offer better parallel performance.Parameters | |
---|---|
classifier |
Function : the classifier function mapping input elements to keys |
Returns | |
---|---|
Collector<T, ?, Map<K, List<T>>> |
a Collector implementing the group-by operation |
Collector<T, ?, M> groupingBy (Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
返回一个Collector
,对T
类型的输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游Collector
对与给定键相关联的值执行简化操作。 收集器生成的Map
是使用提供的工厂功能创建的。
分类功能将元素映射到某些关键类型K
。 下游收集器上类型的元素进行操作T
并产生类型的结果D
。 由此产生的收集器产生Map<K, D>
。
例如,要计算每个城市中姓名的排序集合:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
mapping(Person::getLastName, toSet())));
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements are presented to the downstream collector is not required, using groupingByConcurrent(Function, Supplier, Collector)
may offer better parallel performance.Parameters | |
---|---|
classifier |
Function : a classifier function mapping input elements to keys |
mapFactory |
Supplier : a function which, when called, produces a new empty Map of the desired type |
downstream |
Collector : a Collector implementing the downstream reduction |
Returns | |
---|---|
Collector<T, ?, M> |
a Collector implementing the cascaded group-by operation |
Collector<T, ?, Map<K, D>> groupingBy (Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
返回 Collector
“由基团”上的类型的输入元件操作实现级联 T
,根据分类功能分组元素,然后使用下游的指定与给定键相关联的值进行还原操作 Collector
。
分类功能将元素映射到某些关键类型K
。 下游收集器上类型的元素进行操作T
并产生类型的结果D
。 由此产生的收集器产生Map<K, D>
。
对返回的 Map
的类型,可变性,可串行性或线程安全性没有保证。
例如,要计算每个城市中人员的姓氏集合:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements are presented to the downstream collector is not required, using groupingByConcurrent(Function, Collector)
may offer better parallel performance.Parameters | |
---|---|
classifier |
Function : a classifier function mapping input elements to keys |
downstream |
Collector : a Collector implementing the downstream reduction |
Returns | |
---|---|
Collector<T, ?, Map<K, D>> |
a Collector implementing the cascaded group-by operation |
Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent (Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
返回并发 Collector
对类型为 T
输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游 Collector
对与给定键相关联的值执行简化操作。
这是一个 concurrent
和 unordered
收藏家。
分类功能将元素映射到某些关键类型K
。 下游收集器上类型的元素进行操作T
并产生类型的结果D
。 由此产生的收集器产生Map<K, D>
。
例如,要计算每个城市中姓名的排序集合:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingByConcurrent(Person::getCity,
mapping(Person::getLastName, toSet())));
Parameters | |
---|---|
classifier |
Function : a classifier function mapping input elements to keys |
downstream |
Collector : a Collector implementing the downstream reduction |
Returns | |
---|---|
Collector<T, ?, ConcurrentMap<K, D>> |
a concurrent, unordered Collector implementing the cascaded group-by operation |
Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent (Function<? super T, ? extends K> classifier)
返回一个并发的 Collector
,对 T
类型的输入元素执行“group by”操作,根据分类函数对元素进行分组。
这是一个 concurrent
和 unordered
收藏家。
分类功能将元素映射到某些关键类型K
。 该收集器产生一个ConcurrentMap<K, List<T>>
其键值是将分类函数应用于输入元素而得到的值,其对应的值为List
,其包含映射到分类函数List
关键的输入元素。
还有的类型,可变性,或串行化的任何保证 Map
或者 List
对象返回,或的线程安全的 List
对象返回。
groupingByConcurrent(classifier, toList());
Parameters | |
---|---|
classifier |
Function : a classifier function mapping input elements to keys |
Returns | |
---|---|
Collector<T, ?, ConcurrentMap<K, List<T>>> |
a concurrent, unordered Collector implementing the group-by operation |
Collector<T, ?, M> groupingByConcurrent (Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
返回Collector
并发Collector
,对T
类型的输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游Collector
对与给定键相关联的值执行简化操作。 收集器生成的ConcurrentMap
使用提供的工厂功能创建。
这是一个 concurrent
和 unordered
收藏家。
分类功能将元素映射到某些关键类型K
。 下游收集器上类型的元素进行操作T
并产生类型的结果D
。 由此产生的收集器产生Map<K, D>
。
例如,要计算每个城市中姓名的排序集合:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
mapping(Person::getLastName, toSet())));
Parameters | |
---|---|
classifier |
Function : a classifier function mapping input elements to keys |
mapFactory |
Supplier : a function which, when called, produces a new empty ConcurrentMap of the desired type |
downstream |
Collector : a Collector implementing the downstream reduction |
Returns | |
---|---|
Collector<T, ?, M> |
a concurrent, unordered Collector implementing the cascaded group-by operation |
Collector<CharSequence, ?, String> joining (CharSequence delimiter, CharSequence prefix, CharSequence suffix)
返回一个 Collector
,它将以指定的分隔符分隔的输入元素与指定的前缀和后缀以遇到顺序连接起来。
Parameters | |
---|---|
delimiter |
CharSequence : the delimiter to be used between each element |
prefix |
CharSequence : the sequence of characters to be used at the beginning of the joined result |
suffix |
CharSequence : the sequence of characters to be used at the end of the joined result |
Returns | |
---|---|
Collector<CharSequence, ?, String> |
A Collector which concatenates CharSequence elements, separated by the specified delimiter, in encounter order |
Collector<CharSequence, ?, String> joining (CharSequence delimiter)
返回一个 Collector
,按照相遇顺序连接以指定分隔符分隔的输入元素。
Parameters | |
---|---|
delimiter |
CharSequence : the delimiter to be used between each element |
Returns | |
---|---|
Collector<CharSequence, ?, String> |
A Collector which concatenates CharSequence elements, separated by the specified delimiter, in encounter order |
Collector<CharSequence, ?, String> joining ()
返回一个 Collector
,按照相遇顺序将输入元素连接成一个 String
。
Returns | |
---|---|
Collector<CharSequence, ?, String> |
a Collector that concatenates the input elements into a String , in encounter order |
Collector<T, ?, R> mapping (Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)
适应一个 Collector
类型的接受元件 U
到类型的一个接受元件 T
通过积累前应用映射函数到每个输入元素。
mapping()
collectors are most useful when used in a multi-level reduction, such as downstream of a groupingBy
or partitioningBy
. For example, given a stream of Person
, to accumulate the set of last names in each city: Map<City, Set<String>> lastNamesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
Parameters | |
---|---|
mapper |
Function : a function to be applied to the input elements |
downstream |
Collector : a collector which will accept mapped values |
Returns | |
---|---|
Collector<T, ?, R> |
a collector which applies the mapping function to the input elements and provides the mapped results to the downstream collector |
Collector<T, ?, Optional<T>> maxBy (Comparator<? super T> comparator)
返回 Collector
,根据给定的 Comparator
生成最大元素,描述为 Optional<T>
。
reducing(BinaryOperator.maxBy(comparator))
Parameters | |
---|---|
comparator |
Comparator : a Comparator for comparing elements |
Returns | |
---|---|
Collector<T, ?, Optional<T>> |
a Collector that produces the maximal value |
Collector<T, ?, Optional<T>> minBy (Comparator<? super T> comparator)
返回 Collector
,根据给定的 Comparator
生成最小元素,描述为 Optional<T>
。
reducing(BinaryOperator.minBy(comparator))
Parameters | |
---|---|
comparator |
Comparator : a Comparator for comparing elements |
Returns | |
---|---|
Collector<T, ?, Optional<T>> |
a Collector that produces the minimal value |
Collector<T, ?, Map<Boolean, List<T>>> partitioningBy (Predicate<? super T> predicate)
返回Collector
由划分根据所述输入元件Predicate
,并且将它们组织到一个Map<Boolean, List<T>>
。 对返回的Map
的类型,可变性,可串行性或线程安全性没有保证。
Parameters | |
---|---|
predicate |
Predicate : a predicate used for classifying input elements |
Returns | |
---|---|
Collector<T, ?, Map<Boolean, List<T>>> |
a Collector implementing the partitioning operation |
Collector<T, ?, Map<Boolean, D>> partitioningBy (Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
返回 Collector
由划分根据所述输入元件 Predicate
,减少了在根据另一每个分区中的值 Collector
,并且将它们组织到一个 Map<Boolean, D>
其值是下游减少的结果。
对返回的 Map
的类型,可变性,可串行性或线程安全性没有任何保证。
Parameters | |
---|---|
predicate |
Predicate : a predicate used for classifying input elements |
downstream |
Collector : a Collector implementing the downstream reduction |
Returns | |
---|---|
Collector<T, ?, Map<Boolean, D>> |
a Collector implementing the cascaded partitioning operation |
也可以看看:
Collector<T, ?, Optional<T>> reducing (BinaryOperator<T> op)
返回一个Collector
,它在指定的BinaryOperator
下执行缩减它的输入元素。 结果描述为Optional<T>
。
reducing()
collectors are most useful when used in a multi-level reduction, downstream of groupingBy
or partitioningBy
. To perform a simple reduction on a stream, use reduce(BinaryOperator)
instead. 例如,给定一串 Person
,计算每个城市中最高的人:
Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
Map<City, Person> tallestByCity
= people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
Parameters | |
---|---|
op |
BinaryOperator : a BinaryOperator<T> used to reduce the input elements |
Returns | |
---|---|
Collector<T, ?, Optional<T>> |
a Collector which implements the reduction operation |
Collector<T, ?, U> reducing (U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
返回一个Collector
,它在指定的映射函数和BinaryOperator
下执行其输入元素的BinaryOperator
。 这是reducing(Object, BinaryOperator)
的推广,它允许在还原之前转换元素。
reducing()
collectors are most useful when used in a multi-level reduction, downstream of groupingBy
or partitioningBy
. To perform a simple map-reduce on a stream, use map(Function)
and reduce(Object, BinaryOperator)
instead. 例如,给定一串 Person
,计算每个城市居民的最长姓氏:
Comparator<String> byLength = Comparator.comparing(String::length);
Map<City, String> longestLastNameByCity
= people.stream().collect(groupingBy(Person::getCity,
reducing(Person::getLastName, BinaryOperator.maxBy(byLength))));
Parameters | |
---|---|
identity |
U : the identity value for the reduction (also, the value that is returned when there are no input elements) |
mapper |
Function : a mapping function to apply to each input value |
op |
BinaryOperator : a BinaryOperator<U> used to reduce the mapped values |
Returns | |
---|---|
Collector<T, ?, U> |
a Collector implementing the map-reduce operation |
Collector<T, ?, T> reducing (T identity, BinaryOperator<T> op)
返回一个 Collector
,它使用提供的标识在指定的 BinaryOperator
下执行缩减其输入元素。
reducing()
collectors are most useful when used in a multi-level reduction, downstream of groupingBy
or partitioningBy
. To perform a simple reduction on a stream, use reduce(Object, BinaryOperator)
} instead.Parameters | |
---|---|
identity |
T : the identity value for the reduction (also, the value that is returned when there are no input elements) |
op |
BinaryOperator : a BinaryOperator<T> used to reduce the input elements |
Returns | |
---|---|
Collector<T, ?, T> |
a Collector which implements the reduction operation |
Collector<T, ?, DoubleSummaryStatistics> summarizingDouble (ToDoubleFunction<? super T> mapper)
返回一个 Collector
, double
映射函数 Collector
应用于每个输入元素,并返回结果值的汇总统计信息。
Parameters | |
---|---|
mapper |
ToDoubleFunction : a mapping function to apply to each element |
Returns | |
---|---|
Collector<T, ?, DoubleSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
Collector<T, ?, IntSummaryStatistics> summarizingInt (ToIntFunction<? super T> mapper)
返回一个 Collector
, int
映射函数应用于每个输入元素,并返回结果值的汇总统计信息。
Parameters | |
---|---|
mapper |
ToIntFunction : a mapping function to apply to each element |
Returns | |
---|---|
Collector<T, ?, IntSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
Collector<T, ?, LongSummaryStatistics> summarizingLong (ToLongFunction<? super T> mapper)
返回一个 Collector
, long
映射函数 Collector
应用于每个输入元素,并返回结果值的摘要统计信息。
Parameters | |
---|---|
mapper |
ToLongFunction : the mapping function to apply to each element |
Returns | |
---|---|
Collector<T, ?, LongSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
Collector<T, ?, Double> summingDouble (ToDoubleFunction<? super T> mapper)
返回Collector
,该值产生应用于输入元素的双值函数的总和。 如果没有元素存在,结果为0。
返回的总数可能因记录数值的顺序而有所不同,这是由于累加的舍入误差以及不同大小的数值。 按增加绝对值排序的值倾向于产生更准确的结果。 如果任何记录的值是NaN
或总和在任何一点NaN
那么总和将是NaN
。
Parameters | |
---|---|
mapper |
ToDoubleFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
Collector<T, ?, Integer> summingInt (ToIntFunction<? super T> mapper)
返回一个Collector
,它产生应用于输入元素的整数值函数的和。 如果没有元素存在,结果为0。
Parameters | |
---|---|
mapper |
ToIntFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Integer> |
a Collector that produces the sum of a derived property |
Collector<T, ?, Long> summingLong (ToLongFunction<? super T> mapper)
返回一个Collector
,它产生应用于输入元素的长值函数的总和。 如果没有元素存在,结果为0。
Parameters | |
---|---|
mapper |
ToLongFunction : a function extracting the property to be summed |
Returns | |
---|---|
Collector<T, ?, Long> |
a Collector that produces the sum of a derived property |
Collector<T, ?, C> toCollection (Supplier<C> collectionFactory)
返回一个Collector
,按照相遇顺序将输入元素累加到新的Collection
中。 Collection
由提供的工厂创建。
Parameters | |
---|---|
collectionFactory |
Supplier : a Supplier which returns a new, empty Collection of the appropriate type |
Returns | |
---|---|
Collector<T, ?, C> |
a Collector which collects all the input elements into a Collection , in encounter order |
Collector<T, ?, M> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回并发的 Collector
,它将元素累加到 ConcurrentMap
其键和值是将提供的映射函数应用于输入元素的结果。
如果映射键包含重复项(根据equals(Object)
),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。 ConcurrentMap
由提供的供应商功能创建。
这是一个 concurrent
和 unordered
收藏家。
Parameters | |
---|---|
keyMapper |
Function : a mapping function to produce keys |
valueMapper |
Function : a mapping function to produce values |
mergeFunction |
BinaryOperator : a merge function, used to resolve collisions between values associated with the same key, as supplied to merge(Object, Object, BiFunction) |
mapSupplier |
Supplier : a function which returns a new, empty Map into which the results will be inserted |
Returns | |
---|---|
Collector<T, ?, M> |
a concurrent, unordered Collector which collects elements into a ConcurrentMap whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to all input elements equal to the key and combining them using the merge function |
Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回并发的 Collector
,它将元素累加到 ConcurrentMap
其键和值是将提供的映射函数应用于输入元素的结果。
如果映射键包含重复项(根据 equals(Object)
),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。
toConcurrentMap
simply use a merge function that throws unconditionally, but you can easily write more flexible merge policies. For example, if you have a stream of Person
, and you want to produce a "phone book" mapping name to address, but it is possible that two persons have the same name, you can do as follows to gracefully deals with these collisions, and produce a Map
mapping names to a concatenated list of addresses: Map<String, String> phoneBook
people.stream().collect(toConcurrentMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
这是一个 concurrent
和 unordered
收藏家。
Parameters | |
---|---|
keyMapper |
Function : a mapping function to produce keys |
valueMapper |
Function : a mapping function to produce values |
mergeFunction |
BinaryOperator : a merge function, used to resolve collisions between values associated with the same key, as supplied to merge(Object, Object, BiFunction) |
Returns | |
---|---|
Collector<T, ?, ConcurrentMap<K, U>> |
a concurrent, unordered Collector which collects elements into a ConcurrentMap whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to all input elements equal to the key and combining them using the merge function |
Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
返回将元素累加到 ConcurrentMap
中的并发 Collector
,其键和值是将提供的映射函数应用于输入元素的结果。
如果映射的密钥包含重复项(根据equals(Object)
),则执行收集操作时将引发IllegalStateException
。 如果映射的密钥可能有重复项, toConcurrentMap(Function, Function, BinaryOperator)
改为使用toConcurrentMap(Function, Function, BinaryOperator)
。
identity()
may be helpful. For example, the following produces a Map
mapping students to their grade point average: Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
And the following produces a Map
mapping a unique identifier to students: Map<String, Student> studentIdToStudent
students.stream().collect(toConcurrentMap(Student::getId,
Functions.identity());
这是一个 concurrent
和 unordered
收藏家。
Parameters | |
---|---|
keyMapper |
Function : the mapping function to produce keys |
valueMapper |
Function : the mapping function to produce values |
Returns | |
---|---|
Collector<T, ?, ConcurrentMap<K, U>> |
a concurrent, unordered Collector which collects elements into a ConcurrentMap whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to the input elements |
Collector<T, ?, List<T>> toList ()
返回一个Collector
,将输入元素累加到新的List
。 对返回的List
的类型,可变性,可串行性或线程安全性没有保证; 如果需要对返回的List
更多控制,请使用toCollection(Supplier)
。
Returns | |
---|---|
Collector<T, ?, List<T>> |
a Collector which collects all the input elements into a List , in encounter order |
Collector<T, ?, Map<K, U>> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个 Collector
,它将元素累加到 Map
其键和值是将提供的映射函数应用于输入元素的结果。
如果映射键包含重复项(根据 equals(Object)
),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。
toMap
simply use a merge function that throws unconditionally, but you can easily write more flexible merge policies. For example, if you have a stream of Person
, and you want to produce a "phone book" mapping name to address, but it is possible that two persons have the same name, you can do as follows to gracefully deals with these collisions, and produce a Map
mapping names to a concatenated list of addresses: Map<String, String> phoneBook
people.stream().collect(toMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are merged into the Map
in encounter order, using toConcurrentMap(Function, Function, BinaryOperator)
may offer better parallel performance.Parameters | |
---|---|
keyMapper |
Function : a mapping function to produce keys |
valueMapper |
Function : a mapping function to produce values |
mergeFunction |
BinaryOperator : a merge function, used to resolve collisions between values associated with the same key, as supplied to merge(Object, Object, BiFunction) |
Returns | |
---|---|
Collector<T, ?, Map<K, U>> |
a Collector which collects elements into a Map whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to all input elements equal to the key and combining them using the merge function |
Collector<T, ?, M> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回一个 Collector
,它将元素累加到 Map
其键和值是将提供的映射函数应用于输入元素的结果。
如果映射键包含重复项(根据equals(Object)
),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。 Map
由提供的供应商功能创建。
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are merged into the Map
in encounter order, using toConcurrentMap(Function, Function, BinaryOperator, Supplier)
may offer better parallel performance.Parameters | |
---|---|
keyMapper |
Function : a mapping function to produce keys |
valueMapper |
Function : a mapping function to produce values |
mergeFunction |
BinaryOperator : a merge function, used to resolve collisions between values associated with the same key, as supplied to merge(Object, Object, BiFunction) |
mapSupplier |
Supplier : a function which returns a new, empty Map into which the results will be inserted |
Returns | |
---|---|
Collector<T, ?, M> |
a Collector which collects elements into a Map whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to all input elements equal to the key and combining them using the merge function |
Collector<T, ?, Map<K, U>> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
返回一个 Collector
,它将元素累加到 Map
其键和值是将提供的映射函数应用于输入元素的结果。
如果映射密钥包含重复项(根据equals(Object)
),则执行收集操作时将引发IllegalStateException
。 如果映射的密钥可能有重复项, toMap(Function, Function, BinaryOperator)
改为使用toMap(Function, Function, BinaryOperator)
。
identity()
may be helpful. For example, the following produces a Map
mapping students to their grade point average: Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
And the following produces a Map
mapping a unique identifier to students: Map<String, Student> studentIdToStudent
students.stream().collect(toMap(Student::getId,
Functions.identity());
Collector
is not concurrent. For parallel stream pipelines, the combiner
function operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are inserted into the Map
in encounter order, using toConcurrentMap(Function, Function)
may offer better parallel performance.Parameters | |
---|---|
keyMapper |
Function : a mapping function to produce keys |
valueMapper |
Function : a mapping function to produce values |
Returns | |
---|---|
Collector<T, ?, Map<K, U>> |
a Collector which collects elements into a Map whose keys and values are the result of applying mapping functions to the input elements |
Collector<T, ?, Set<T>> toSet ()
返回一个Collector
,将输入元素累加到新的Set
。 Set
返回的Set
的类型,可变性,可串行性或线程安全性没有保证; 如果需要对返回的Set
更多控制,请使用toCollection(Supplier)
。
这是一个 unordered
收藏家。
Returns | |
---|---|
Collector<T, ?, Set<T>> |
a Collector which collects all the input elements into a Set |