Most visited

Recently visited

Added in API level 24

Collectors

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));

 

Summary

Public methods

static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper)

返回一个 Collector ,它产生应用于输入元素的双值函数的算术平均值。

static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)

返回一个 Collector ,它产生应用于输入元素的整数值函数的算术平均值。

static <T> Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)

返回一个 Collector ,它生成应用于输入元素的长 Collector函数的算术平均值。

static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)

Collector以执行额外的修整转换。

static <T> Collector<T, ?, Long> counting()

返回 Collector类型的接受元件 T计数输入元件的数量。

static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)

返回一个 Collector ,对 T类型的输入元素执行“group by”操作,根据分类函数对元素进行分组,然后将结果返回到 Map

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)

返回 Collector “由基团”上的类型的输入元件操作实现级联 T ,根据分类功能分组元素,然后使用下游的指定与给定键相关联的值进行还原操作 Collector

static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)

返回 Collector “由基团”上的类型的输入元件操作实现级联 T ,根据分类功能分组元素,然后使用下游的指定与给定键相关联的值进行还原操作 Collector

static <T, K, A, D> Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)

返回并发 Collector对类型为 T输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游 Collector对与给定键相关联的值执行简化操作。

static <T, K> Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent(Function<? super T, ? extends K> classifier)

返回一个并发的 Collector ,对类型为 T输入元素执行“group by”操作,根据分类函数对元素进行分组。

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)

返回并发 CollectorT类型的输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游 Collector对与给定键相关联的值执行简化操作。

static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

返回一个 Collector ,它将输入元素(用指定的分隔符分隔)与指定的前缀和后缀按照遇到顺序连接起来。

static Collector<CharSequence, ?, String> joining(CharSequence delimiter)

返回 Collector ,按照相遇顺序连接输入元素,由指定的分隔符分隔。

static Collector<CharSequence, ?, String> joining()

返回一个 Collector ,按照遇到顺序将输入元素连接成一个 String

static <T, U, A, R> Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)

适应一个 Collector类型的接受元件 U到类型的一个接受元件 T通过积累前应用映射函数到每个输入元素。

static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)

返回 Collector ,根据给定的 Comparator生成最大元素,描述为 Optional<T>

static <T> Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)

返回一个 Collector ,它根据给定的 Comparator生成最小元素,描述为 Optional<T>

static <T> Collector<T, ?, Map<BooleanList<T>>> partitioningBy(Predicate<? super T> predicate)

返回 Collector由划分根据所述输入元件 Predicate ,并且将它们组织到一个 Map<Boolean, List<T>>

static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

返回根据 Collector分割输入元素的 Predicate ,根据另一个 Collector减少每个分区中的值,并将它们组织为 Map<Boolean, D>其值是下游减少的结果。

static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)

返回 Collector ,它在指定的 BinaryOperator下执行其输入元素的 BinaryOperator

static <T, U> Collector<T, ?, U> reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)

返回一个 Collector ,它在指定的映射函数和 BinaryOperator下执行缩减它的输入元素。

static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op)

返回一个 Collector ,它使用提供的标识在指定的 BinaryOperator下执行其输入元素的 BinaryOperator

static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)

返回一个 Collectordouble映射函数 Collector应用于每个输入元素,并返回结果值的汇总统计信息。

static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)

返回一个 Collectorint映射函数 Collector应用于每个输入元素,并返回结果值的摘要统计信息。

static <T> Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)

返回一个 Collectorlong映射函数 Collector应用于每个输入元素,并返回结果值的汇总统计信息。

static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)

返回一个 Collector ,它产生应用于输入元素的双值函数的总和。

static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)

返回 Collector ,该值产生应用于输入元素的整数值函数的和。

static <T> Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper)

返回一个 Collector ,它产生应用于输入元素的长值函数的总和。

static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)

返回一个 Collector ,按照相遇顺序将输入元素累加到新的 Collection中。

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)

返回并发的 Collector ,它将元素累加到 ConcurrentMap其键和值是将提供的映射函数应用于输入元素的结果。

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)

返回并发的 Collector ,它将元素累加到 ConcurrentMap其键和值是将提供的映射函数应用于输入元素的结果。

static <T, K, U> Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

返回并发的 Collector ,将元素累加到 ConcurrentMap其键和值是将提供的映射函数应用于输入元素的结果。

static <T> Collector<T, ?, List<T>> toList()

返回一个 Collector ,将输入元素累加到新的 List

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)

返回一个 Collector ,其累积元件成 Map其键和值施加提供映射函数的输入元素的结果。

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)

返回一个 Collector ,它将元素累加到 Map其键和值是将提供的映射函数应用于输入元素的结果。

static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

返回一个 Collector ,将元素累加到 Map其键和值是将提供的映射函数应用于输入元素的结果。

static <T> Collector<T, ?, Set<T>> toSet()

返回一个 Collector ,将输入元素累加到新的 Set

Inherited methods

From class java.lang.Object

Public methods

averagingDouble

Added in API level 24
Collector<T, ?, Double> averagingDouble (ToDoubleFunction<? super T> mapper)

返回Collector ,该值产生应用于输入元素的双值函数的算术平均值。 如果没有元素存在,结果为0。

返回的平均值可能会因记录值的顺序而有所不同,这是由于累加的舍入误差以及不同数值的值。 按增加绝对值排序的值倾向于产生更准确的结果。 如果任何记录的值是NaN或总和在任何一点NaN那么平均值将是NaN

Implementation Note:
  • The 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

averagingInt

Added in API level 24
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

averagingLong

Added in API level 24
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

collectingAndThen

Added in API level 24
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

counting

Added in API level 24
Collector<T, ?, Long> counting ()

返回Collector类型的接受元件T计数输入元件的数量。 如果没有元素存在,结果为0。

实现要求:
  • This produces a result equivalent to:
    reducing(0L, e -> 1L, Long::sum)
     
Returns
Collector<T, ?, Long> a Collector that counts the input elements

groupingBy

Added in API level 24
Collector<T, ?, Map<K, List<T>>> groupingBy (Function<? super T, ? extends K> classifier)

返回 Collector “由基团”上的类型的输入元件操作实现 T ,根据分类功能分组元素,并且在返回的结果 Map

分类功能将元素映射到某些关键类型K 收集器产生一个Map<K, List<T>>其键值是将分类函数应用于输入元素所得到的值,其对应的值为List其中包含映射到分类函数下相关键值的输入元素。

对于返回的 MapList对象的类型,可变性,可串行性或线程安全性没有保证。

实现要求:
  • This produces a result similar to:
    groupingBy(classifier, toList());
     
Implementation Note:
  • The returned 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

也可以看看:

groupingBy

Added in API level 24
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())));
 

Implementation Note:
  • The returned 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

也可以看看:

groupingBy

Added in API level 24
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())));
 

Implementation Note:
  • The returned 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

也可以看看:

groupingByConcurrent

Added in API level 24
Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent (Function<? super T, ? extends K> classifier, 
                Collector<? super T, A, D> downstream)

返回并发 Collector对类型为 T输入元素执行级联的“group by”操作,根据分类函数对元素进行分组,然后使用指定的下游 Collector对与给定键相关联的值执行简化操作。

这是一个 concurrentunordered收藏家。

分类功能将元素映射到某些关键类型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

也可以看看:

groupingByConcurrent

Added in API level 24
Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent (Function<? super T, ? extends K> classifier)

返回一个并发的 Collector ,对 T类型的输入元素执行“group by”操作,根据分类函数对元素进行分组。

这是一个 concurrentunordered收藏家。

分类功能将元素映射到某些关键类型K 该收集器产生一个ConcurrentMap<K, List<T>>其键值是将分类函数应用于输入元素而得到的值,其对应的值为List ,其包含映射到分类函数List关键的输入元素。

还有的类型,可变性,或串行化的任何保证 Map或者 List对象返回,或的线程安全的 List对象返回。

实现要求:
  • This produces a result similar to:
    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

也可以看看:

groupingByConcurrent

Added in API level 24
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使用提供的工厂功能创建。

这是一个 concurrentunordered收藏家。

分类功能将元素映射到某些关键类型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

也可以看看:

joining

Added in API level 24
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

joining

Added in API level 24
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

joining

Added in API level 24
Collector<CharSequence, ?, String> joining ()

返回一个 Collector ,按照相遇顺序将输入元素连接成一个 String

Returns
Collector<CharSequence, ?, String> a Collector that concatenates the input elements into a String, in encounter order

mapping

Added in API level 24
Collector<T, ?, R> mapping (Function<? super T, ? extends U> mapper, 
                Collector<? super U, A, R> downstream)

适应一个 Collector类型的接受元件 U到类型的一个接受元件 T通过积累前应用映射函数到每个输入元素。

API Note:
  • The 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

maxBy

Added in API level 24
Collector<T, ?, Optional<T>> maxBy (Comparator<? super T> comparator)

返回 Collector ,根据给定的 Comparator生成最大元素,描述为 Optional<T>

实现要求:
  • This produces a result equivalent to:
    reducing(BinaryOperator.maxBy(comparator))
     
Parameters
comparator Comparator: a Comparator for comparing elements
Returns
Collector<T, ?, Optional<T>> a Collector that produces the maximal value

minBy

Added in API level 24
Collector<T, ?, Optional<T>> minBy (Comparator<? super T> comparator)

返回 Collector ,根据给定的 Comparator生成最小元素,描述为 Optional<T>

实现要求:
  • This produces a result equivalent to:
    reducing(BinaryOperator.minBy(comparator))
     
Parameters
comparator Comparator: a Comparator for comparing elements
Returns
Collector<T, ?, Optional<T>> a Collector that produces the minimal value

partitioningBy

Added in API level 24
Collector<T, ?, Map<BooleanList<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<BooleanList<T>>> a Collector implementing the partitioning operation

也可以看看:

partitioningBy

Added in API level 24
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

也可以看看:

reducing

Added in API level 24
Collector<T, ?, Optional<T>> reducing (BinaryOperator<T> op)

返回一个Collector ,它在指定的BinaryOperator下执行缩减它的输入元素。 结果描述为Optional<T>

API Note:
  • The 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

也可以看看:

reducing

Added in API level 24
Collector<T, ?, U> reducing (U identity, 
                Function<? super T, ? extends U> mapper, 
                BinaryOperator<U> op)

返回一个Collector ,它在指定的映射函数和BinaryOperator下执行其输入元素的BinaryOperator 这是reducing(Object, BinaryOperator)的推广,它允许在还原之前转换元素。

API Note:
  • The 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

也可以看看:

reducing

Added in API level 24
Collector<T, ?, T> reducing (T identity, 
                BinaryOperator<T> op)

返回一个 Collector ,它使用提供的标识在指定的 BinaryOperator下执行缩减其输入元素。

API Note:
  • The 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

也可以看看:

summarizingDouble

Added in API level 24
Collector<T, ?, DoubleSummaryStatistics> summarizingDouble (ToDoubleFunction<? super T> mapper)

返回一个 Collectordouble映射函数 Collector应用于每个输入元素,并返回结果值的汇总统计信息。

Parameters
mapper ToDoubleFunction: a mapping function to apply to each element
Returns
Collector<T, ?, DoubleSummaryStatistics> a Collector implementing the summary-statistics reduction

也可以看看:

summarizingInt

Added in API level 24
Collector<T, ?, IntSummaryStatistics> summarizingInt (ToIntFunction<? super T> mapper)

返回一个 Collectorint映射函数应用于每个输入元素,并返回结果值的汇总统计信息。

Parameters
mapper ToIntFunction: a mapping function to apply to each element
Returns
Collector<T, ?, IntSummaryStatistics> a Collector implementing the summary-statistics reduction

也可以看看:

summarizingLong

Added in API level 24
Collector<T, ?, LongSummaryStatistics> summarizingLong (ToLongFunction<? super T> mapper)

返回一个 Collectorlong映射函数 Collector应用于每个输入元素,并返回结果值的摘要统计信息。

Parameters
mapper ToLongFunction: the mapping function to apply to each element
Returns
Collector<T, ?, LongSummaryStatistics> a Collector implementing the summary-statistics reduction

也可以看看:

summingDouble

Added in API level 24
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

summingInt

Added in API level 24
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

summingLong

Added in API level 24
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

toCollection

Added in API level 24
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

toConcurrentMap

Added in API level 24
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由提供的供应商功能创建。

这是一个 concurrentunordered收藏家。

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

也可以看看:

toConcurrentMap

Added in API level 24
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) ),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。

API Note:
  • There are multiple ways to deal with collisions between multiple elements mapping to the same key. The other forms of 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));
     

    这是一个 concurrentunordered收藏家。

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

也可以看看:

toConcurrentMap

Added in API level 24
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)

API Note:
  • It is common for either the key or the value to be the input elements. In this case, the utility method 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());
     

    这是一个 concurrentunordered收藏家。

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

也可以看看:

toList

Added in API level 24
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

toMap

Added in API level 24
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) ),则将值映射函数应用于每个相等元素,并使用提供的合并函数合并结果。

API Note:
  • There are multiple ways to deal with collisions between multiple elements mapping to the same key. The other forms of 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));
     
Implementation Note:
  • The returned 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

也可以看看:

toMap

Added in API level 24
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由提供的供应商功能创建。

Implementation Note:
  • The returned 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

也可以看看:

toMap

Added in API level 24
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)

API Note:
  • It is common for either the key or the value to be the input elements. In this case, the utility method 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());
     
Implementation Note:
  • The returned 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

也可以看看:

toSet

Added in API level 24
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

Hooray!