public final class Collectors extends Object
Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc. The following are examples of using the predefined collectors to perform common mutable reduction tasks:
 // 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));
 | Modifier and Type | Method | Description | 
|---|---|---|
| static <T> Collector | averagingDouble | Returns a  Collectorthat produces the arithmetic mean of a double-valued function applied to the input elements. | 
| static <T> Collector | averagingInt | Returns a  Collectorthat produces the arithmetic mean of an integer-valued function applied to the input elements. | 
| static <T> Collector | averagingLong | Returns a  Collectorthat produces the arithmetic mean of a long-valued function applied to the input elements. | 
| static <T, | collectingAndThen | Adapts a  Collectorto perform an additional finishing transformation. | 
| static <T> Collector | counting() | Returns a  Collectoraccepting elements of typeTthat counts the number of input elements. | 
| static <T, | filtering | Adapts a  Collectorto one accepting elements of the same typeTby applying the predicate to each input element and only accumulating if the predicate returnstrue. | 
| static <T, | flatMapping | Adapts a  Collectoraccepting elements of typeUto one accepting elements of typeTby applying a flat mapping function to each input element before accumulation. | 
| static <T, | groupingBy | Returns a  Collectorimplementing a "group by" operation on input elements of typeT, grouping elements according to a classification function, and returning the results in aMap. | 
| static <T, | groupingBy | Returns a  Collectorimplementing a cascaded "group by" operation on input elements of typeT, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector. | 
| static <T, | groupingBy | Returns a  Collectorimplementing a cascaded "group by" operation on input elements of typeT, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector. | 
| static <T, | groupingByConcurrent | Returns a concurrent  Collectorimplementing a "group by" operation on input elements of typeT, grouping elements according to a classification function. | 
| static <T, | groupingByConcurrent | Returns a concurrent  Collectorimplementing a cascaded "group by" operation on input elements of typeT, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector. | 
| static <T, | groupingByConcurrent | Returns a concurrent  Collectorimplementing a cascaded "group by" operation on input elements of typeT, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstreamCollector. | 
| static Collector | joining() | Returns a  Collectorthat concatenates the input elements into aString, in encounter order. | 
| static Collector | joining | Returns a  Collectorthat concatenates the input elements, separated by the specified delimiter, in encounter order. | 
| static Collector | joining | Returns a  Collectorthat concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order. | 
| static <T, | mapping | Adapts a  Collectoraccepting elements of typeUto one accepting elements of typeTby applying a mapping function to each input element before accumulation. | 
| static <T> Collector | maxBy | Returns a  Collectorthat produces the maximal element according to a givenComparator, described as anOptional<T>. | 
| static <T> Collector | minBy | Returns a  Collectorthat produces the minimal element according to a givenComparator, described as anOptional<T>. | 
| static <T> Collector | partitioningBy | Returns a  Collectorwhich partitions the input elements according to aPredicate, and organizes them into aMap<Boolean, List<T>>. | 
| static <T, | partitioningBy | Returns a  Collectorwhich partitions the input elements according to aPredicate, reduces the values in each partition according to anotherCollector, and organizes them into aMap<Boolean, D>whose values are the result of the downstream reduction. | 
| static <T> Collector | reducing | Returns a  Collectorwhich performs a reduction of its input elements under a specifiedBinaryOperator. | 
| static <T> Collector | reducing | Returns a  Collectorwhich performs a reduction of its input elements under a specifiedBinaryOperatorusing the provided identity. | 
| static <T, | reducing | Returns a  Collectorwhich performs a reduction of its input elements under a specified mapping function andBinaryOperator. | 
| static <T> Collector | summarizingDouble | Returns a  Collectorwhich applies andouble-producing mapping function to each input element, and returns summary statistics for the resulting values. | 
| static <T> Collector | summarizingInt | Returns a  Collectorwhich applies anint-producing mapping function to each input element, and returns summary statistics for the resulting values. | 
| static <T> Collector | summarizingLong | Returns a  Collectorwhich applies anlong-producing mapping function to each input element, and returns summary statistics for the resulting values. | 
| static <T> Collector | summingDouble | Returns a  Collectorthat produces the sum of a double-valued function applied to the input elements. | 
| static <T> Collector | summingInt | Returns a  Collectorthat produces the sum of an integer-valued function applied to the input elements. | 
| static <T> Collector | summingLong | Returns a  Collectorthat produces the sum of a long-valued function applied to the input elements. | 
| static <T, | teeing | Returns a  Collectorthat is a composite of two downstream collectors. | 
| static <T, | toCollection | Returns a  Collectorthat accumulates the input elements into a newCollection, in encounter order. | 
| static <T, | toConcurrentMap | Returns a concurrent  Collectorthat accumulates elements into aConcurrentMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T, | toConcurrentMap | Returns a concurrent  Collectorthat accumulates elements into aConcurrentMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T, | toConcurrentMap | Returns a concurrent  Collectorthat accumulates elements into aConcurrentMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T> Collector | toList() | Returns a  Collectorthat accumulates the input elements into a newList. | 
| static <T, | toMap | Returns a  Collectorthat accumulates elements into aMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T, | toMap | Returns a  Collectorthat accumulates elements into aMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T, | toMap | Returns a  Collectorthat accumulates elements into aMapwhose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T> Collector | toSet() | Returns a  Collectorthat accumulates the input elements into a newSet. | 
| static <T> Collector | toUnmodifiableList() | Returns a  Collectorthat accumulates the input elements into an unmodifiable List in encounter order. | 
| static <T, | toUnmodifiableMap | Returns a  Collectorthat accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T, | toUnmodifiableMap | Returns a  Collectorthat accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elements. | 
| static <T> Collector | toUnmodifiableSet() | Returns a  Collectorthat accumulates the input elements into an unmodifiable Set. | 
public static <T, C extends Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory)
Collector that accumulates the input elements into a new Collection, in encounter order. The Collection is created by the provided factory.T - the type of the input elementsC - the type of the resulting Collection
collectionFactory - a supplier providing a new empty Collection into which the results will be insertedCollector which collects all the input elements into a Collection, in encounter orderpublic static <T> Collector<T,?,List<T>> toList()
Collector that accumulates the input elements into a new List. There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).T - the type of the input elementsCollector which collects all the input elements into a List, in encounter orderpublic static <T> Collector<T,?,List<T>> toUnmodifiableList()
Collector that accumulates the input elements into an unmodifiable List in encounter order. The returned Collector disallows null values and will throw NullPointerException if it is presented with a null value.T - the type of the input elementsCollector that accumulates the input elements into an unmodifiable List in encounter orderpublic static <T> Collector<T,?,Set<T>> toSet()
Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned; if more control over the returned Set is required, use toCollection(Supplier). This is an unordered Collector.
T - the type of the input elementsCollector which collects all the input elements into a Set
public static <T> Collector<T,?,Set<T>> toUnmodifiableSet()
Collector that accumulates the input elements into an unmodifiable Set. The returned Collector disallows null values and will throw NullPointerException if it is presented with a null value. If the input contains duplicate elements, an arbitrary element of the duplicates is preserved. This is an unordered Collector.
T - the type of the input elementsCollector that accumulates the input elements into an unmodifiable Set
public static Collector<CharSequence,?,String> joining()
Collector that concatenates the input elements into a String, in encounter order.Collector that concatenates the input elements into a String, in encounter orderpublic static Collector<CharSequence,?,String> joining(CharSequence delimiter)
Collector that concatenates the input elements, separated by the specified delimiter, in encounter order.delimiter - the delimiter to be used between each elementCollector which concatenates CharSequence elements, separated by the specified delimiter, in encounter orderpublic static Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order.delimiter - the delimiter to be used between each elementprefix - the sequence of characters to be used at the beginning of the joined resultsuffix - the sequence of characters to be used at the end of the joined resultCollector which concatenates CharSequence elements, separated by the specified delimiter, in encounter orderpublic static <T, U, A, R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
Collector accepting elements of type U to one accepting elements of type T by applying a mapping function to each input element before accumulation.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())));
 T - the type of the input elementsU - type of elements accepted by downstream collectorA - intermediate accumulation type of the downstream collectorR - result type of collectormapper - a function to be applied to the input elementsdownstream - a collector which will accept mapped valuespublic static <T, U, A, R> Collector<T,?,R> flatMapping(Function<? super T,? extends Stream<? extends U>> mapper, Collector<? super U,A,R> downstream)
Collector accepting elements of type U to one accepting elements of type T by applying a flat mapping function to each input element before accumulation. The flat mapping function maps an input element to a stream covering zero or more output elements that are then accumulated downstream. Each mapped stream is closed after its contents have been placed downstream. (If a mapped stream is null an empty stream is used, instead.)flatMapping() 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 Order, to accumulate the set of line items for each customer: 
 Map<String, Set<LineItem>> itemsByCustomerName
   = orders.stream().collect(
     groupingBy(Order::getCustomerName,
                flatMapping(order -> order.getLineItems().stream(),
                            toSet())));
 T - the type of the input elementsU - type of elements accepted by downstream collectorA - intermediate accumulation type of the downstream collectorR - result type of collectormapper - a function to be applied to the input elements, which returns a stream of resultsdownstream - a collector which will receive the elements of the stream returned by mapperpublic static <T, A, R> Collector<T,?,R> filtering(Predicate<? super T> predicate, Collector<? super T,A,R> downstream)
Collector to one accepting elements of the same type T by applying the predicate to each input element and only accumulating if the predicate returns true.filtering() 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 Employee, to accumulate the employees in each department that have a salary above a certain threshold: 
 Map<Department, Set<Employee>> wellPaidEmployeesByDepartment
   = employees.stream().collect(
     groupingBy(Employee::getDepartment,
                filtering(e -> e.getSalary() > 2000,
                          toSet())));
 filter() operation. In this example, suppose there are no employees whose salary is above the threshold in some department. Using a filtering collector as shown above would result in a mapping from that department to an empty Set. If a stream filter() operation were done instead, there would be no mapping for that department at all.T - the type of the input elementsA - intermediate accumulation type of the downstream collectorR - result type of collectorpredicate - a predicate to be applied to the input elementsdownstream - a collector which will accept values that match the predicatepublic static <T, A, R, RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
Collector to perform an additional finishing transformation. For example, one could adapt the toList() collector to always produce an immutable list with: 
 List<String> list = people.stream().collect(
   collectingAndThen(toList(),
                     Collections::unmodifiableList));
 T - the type of the input elementsA - intermediate accumulation type of the downstream collectorR - result type of the downstream collectorRR - result type of the resulting collectordownstream - a collectorfinisher - a function to be applied to the final result of the downstream collectorpublic static <T> Collector<T,?,Long> counting()
Collector accepting elements of type T that counts the number of input elements. If no elements are present, the result is 0.
     reducing(0L, e -> 1L, Long::sum)
 T - the type of the input elementsCollector that counts the input elementspublic static <T> Collector<T,?,Optional<T>> minBy(Comparator<? super T> comparator)
Collector that produces the minimal element according to a given Comparator, described as an Optional<T>.
     reducing(BinaryOperator.minBy(comparator))
 T - the type of the input elementscomparator - a Comparator for comparing elementsCollector that produces the minimal valuepublic static <T> Collector<T,?,Optional<T>> maxBy(Comparator<? super T> comparator)
Collector that produces the maximal element according to a given Comparator, described as an Optional<T>.
     reducing(BinaryOperator.maxBy(comparator))
 T - the type of the input elementscomparator - a Comparator for comparing elementsCollector that produces the maximal valuepublic static <T> Collector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper)
Collector that produces the sum of an integer-valued function applied to the input elements. If no elements are present, the result is 0.T - the type of the input elementsmapper - a function extracting the property to be summedCollector that produces the sum of a derived propertypublic static <T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)
Collector that produces the sum of a long-valued function applied to the input elements. If no elements are present, the result is 0.T - the type of the input elementsmapper - a function extracting the property to be summedCollector that produces the sum of a derived propertypublic static <T> Collector<T,?,Double> summingDouble(ToDoubleFunction<? super T> mapper)
Collector that produces the sum of a double-valued function applied to the input elements. If no elements are present, the result is 0. The sum returned can vary depending upon the order in which values are recorded, due to accumulated rounding error in addition of values of differing magnitudes. Values sorted by increasing absolute magnitude tend to yield more accurate results. If any recorded value is a NaN or the sum is at any point a NaN then the sum will be NaN.
T - the type of the input elementsmapper - a function extracting the property to be summedCollector that produces the sum of a derived propertypublic static <T> Collector<T,?,Double> averagingInt(ToIntFunction<? super T> mapper)
Collector that produces the arithmetic mean of an integer-valued function applied to the input elements. If no elements are present, the result is 0.T - the type of the input elementsmapper - a function extracting the property to be averagedCollector that produces the arithmetic mean of a derived propertypublic static <T> Collector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper)
Collector that produces the arithmetic mean of a long-valued function applied to the input elements. If no elements are present, the result is 0.T - the type of the input elementsmapper - a function extracting the property to be averagedCollector that produces the arithmetic mean of a derived propertypublic static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)
Collector that produces the arithmetic mean of a double-valued function applied to the input elements. If no elements are present, the result is 0. The average returned can vary depending upon the order in which values are recorded, due to accumulated rounding error in addition of values of differing magnitudes. Values sorted by increasing absolute magnitude tend to yield more accurate results. If any recorded value is a NaN or the sum is at any point a NaN then the average will be 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.T - the type of the input elementsmapper - a function extracting the property to be averagedCollector that produces the arithmetic mean of a derived propertypublic static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op)
Collector which performs a reduction of its input elements under a specified BinaryOperator using the provided identity.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 Stream.reduce(Object, BinaryOperator)} instead.T - element type for the input and output of the reductionidentity - the identity value for the reduction (also, the value that is returned when there are no input elements)op - a BinaryOperator<T> used to reduce the input elementsCollector which implements the reduction operationpublic static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op)
Collector which performs a reduction of its input elements under a specified BinaryOperator. The result is described as an 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 Stream.reduce(BinaryOperator) instead. For example, given a stream of Person, to calculate tallest person in each city: 
 Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
 Map<City, Optional<Person>> tallestByCity
   = people.stream().collect(
     groupingBy(Person::getCity,
                reducing(BinaryOperator.maxBy(byHeight))));
 T - element type for the input and output of the reductionop - a BinaryOperator<T> used to reduce the input elementsCollector which implements the reduction operationpublic static <T, U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op)
Collector which performs a reduction of its input elements under a specified mapping function and BinaryOperator. This is a generalization of reducing(Object, BinaryOperator) which allows a transformation of the elements before reduction.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 Stream.map(Function) and Stream.reduce(Object, BinaryOperator) instead. For example, given a stream of Person, to calculate the longest last name of residents in each city: 
 Comparator<String> byLength = Comparator.comparing(String::length);
 Map<City, String> longestLastNameByCity
   = people.stream().collect(
     groupingBy(Person::getCity,
                reducing("",
                         Person::getLastName,
                         BinaryOperator.maxBy(byLength))));
 T - the type of the input elementsU - the type of the mapped valuesidentity - the identity value for the reduction (also, the value that is returned when there are no input elements)mapper - a mapping function to apply to each input valueop - a BinaryOperator<U> used to reduce the mapped valuesCollector implementing the map-reduce operationpublic static <T, K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
Collector implementing a "group by" operation on input elements of type T, grouping elements according to a classification function, and returning the results in a Map. The classification function maps elements to some key type K. The collector produces a Map<K, List<T>> whose keys are the values resulting from applying the classification function to the input elements, and whose corresponding values are Lists containing the input elements which map to the associated key under the classification function. 
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List objects returned.
     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.T - the type of the input elementsK - the type of the keysclassifier - the classifier function mapping input elements to keysCollector implementing the group-by operationpublic static <T, K, A, D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector. The classification function maps elements to some key type K. The downstream collector operates on elements of type T and produces a result of type D. The resulting collector produces a Map<K, D>. 
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned. 
For example, to compute the set of last names of people in each city:
 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.T - the type of the input elementsK - the type of the keysA - the intermediate accumulation type of the downstream collectorD - the result type of the downstream reductionclassifier - a classifier function mapping input elements to keysdownstream - a Collector implementing the downstream reductionCollector implementing the cascaded group-by operationpublic 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 implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector. The Map produced by the Collector is created with the supplied factory function. The classification function maps elements to some key type K. The downstream collector operates on elements of type T and produces a result of type D. The resulting collector produces a Map<K, D>. 
For example, to compute the set of last names of people in each city, where the city names are sorted:
 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.T - the type of the input elementsK - the type of the keysD - the result type of the downstream reductionA - the intermediate accumulation type of the downstream collectorM - the type of the resulting Map
classifier - a classifier function mapping input elements to keysmapFactory - a supplier providing a new empty Map into which the results will be inserteddownstream - a Collector implementing the downstream reductionCollector implementing the cascaded group-by operationpublic static <T, K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier)
Collector implementing a "group by" operation on input elements of type T, grouping elements according to a classification function. This is a concurrent and unordered Collector. 
The classification function maps elements to some key type K. The collector produces a ConcurrentMap<K, List<T>> whose keys are the values resulting from applying the classification function to the input elements, and whose corresponding values are Lists containing the input elements which map to the associated key under the classification function. 
There are no guarantees on the type, mutability, or serializability of the ConcurrentMap or List objects returned, or of the thread-safety of the List objects returned.
     groupingByConcurrent(classifier, toList());
 T - the type of the input elementsK - the type of the keysclassifier - a classifier function mapping input elements to keysCollector implementing the group-by operationpublic static <T, K, A, D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector. This is a concurrent and unordered Collector. 
The classification function maps elements to some key type K. The downstream collector operates on elements of type T and produces a result of type D. The resulting collector produces a ConcurrentMap<K, D>. 
There are no guarantees on the type, mutability, or serializability of the ConcurrentMap returned. 
For example, to compute the set of last names of people in each city, where the city names are sorted:
 ConcurrentMap<City, Set<String>> namesByCity
   = people.stream().collect(
     groupingByConcurrent(Person::getCity,
                          mapping(Person::getLastName,
                                  toSet())));
 T - the type of the input elementsK - the type of the keysA - the intermediate accumulation type of the downstream collectorD - the result type of the downstream reductionclassifier - a classifier function mapping input elements to keysdownstream - a Collector implementing the downstream reductionCollector implementing the cascaded group-by operationpublic 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)
Collector implementing a cascaded "group by" operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector. The ConcurrentMap produced by the Collector is created with the supplied factory function. This is a concurrent and unordered Collector. 
The classification function maps elements to some key type K. The downstream collector operates on elements of type T and produces a result of type D. The resulting collector produces a ConcurrentMap<K, D>. 
For example, to compute the set of last names of people in each city, where the city names are sorted:
 ConcurrentMap<City, Set<String>> namesByCity
   = people.stream().collect(
     groupingByConcurrent(Person::getCity,
                          ConcurrentSkipListMap::new,
                          mapping(Person::getLastName,
                                  toSet())));
 T - the type of the input elementsK - the type of the keysA - the intermediate accumulation type of the downstream collectorD - the result type of the downstream reductionM - the type of the resulting ConcurrentMap
classifier - a classifier function mapping input elements to keysmapFactory - a supplier providing a new empty ConcurrentMap into which the results will be inserteddownstream - a Collector implementing the downstream reductionCollector implementing the cascaded group-by operationpublic static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate)
Collector which partitions the input elements according to a Predicate, and organizes them into a Map<Boolean, List<T>>. The returned Map always contains mappings for both false and true keys. There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List returned.T - the type of the input elementspredicate - a predicate used for classifying input elementsCollector implementing the partitioning operationpublic static <T, D, A> Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
Collector which partitions the input elements according to a Predicate, reduces the values in each partition according to another Collector, and organizes them into a Map<Boolean, D> whose values are the result of the downstream reduction.  The returned Map always contains mappings for both false and true keys. There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.
T - the type of the input elementsD - the result type of the downstream reductionA - the intermediate accumulation type of the downstream collectorpredicate - a predicate used for classifying input elementsdownstream - a Collector implementing the downstream reductionCollector implementing the cascaded partitioning operationpublic static <T, K, U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), an IllegalStateException is thrown when the collection operation is performed. If the mapped keys might have duplicates, use toMap(Function, Function, BinaryOperator) instead. 
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.
Function.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(Function.identity(),
           student -> computeGPA(student)));
 Map mapping a unique identifier to students: 
 Map<String, Student> studentIdToStudent
   = students.stream().collect(
     toMap(Student::getId,
           Function.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.T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - a mapping function to produce keysvalueMapper - a mapping function to produce valuesCollector which collects elements into a Map whose keys and values are the result of applying mapping functions to the input elementspublic static <T, K, U> Collector<T,?,Map<K,U>> toUnmodifiableMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector that accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), an IllegalStateException is thrown when the collection operation is performed. If the mapped keys might have duplicates, use toUnmodifiableMap(Function, Function, BinaryOperator) to handle merging of the values. 
The returned Collector disallows null keys and values. If either mapping function returns null, NullPointerException will be thrown.
T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - a mapping function to produce keys, must be non-nullvalueMapper - a mapping function to produce values, must be non-nullCollector that accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elementsNullPointerException - if either keyMapper or valueMapper is nullpublic 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 that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided merging function. 
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.
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 deal 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.T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - a mapping function to produce keysvalueMapper - a mapping function to produce valuesmergeFunction - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
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 functionpublic static <T, K, U> Collector<T,?,Map<K,U>> toUnmodifiableMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Collector that accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided merging function. 
The returned Collector disallows null keys and values. If either mapping function returns null, NullPointerException will be thrown.
T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - a mapping function to produce keys, must be non-nullvalueMapper - a mapping function to produce values, must be non-nullmergeFunction - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction), must be non-nullCollector that accumulates the input elements into an unmodifiable Map, whose keys and values are the result of applying the provided mapping functions to the input elementsNullPointerException - if the keyMapper, valueMapper, or mergeFunction is nullpublic 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> mapFactory)
Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided merging function. The Map is created by a provided supplier function.
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.T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionM - the type of the resulting Map
keyMapper - a mapping function to produce keysvalueMapper - a mapping function to produce valuesmergeFunction - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
mapFactory - a supplier providing a new empty Map into which the results will be insertedCollector 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 functionpublic static <T, K, U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector that accumulates elements into a ConcurrentMap whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), an IllegalStateException is thrown when the collection operation is performed. If the mapped keys may have duplicates, use toConcurrentMap(Function, Function, BinaryOperator) instead. 
There are no guarantees on the type, mutability, or serializability of the ConcurrentMap returned.
Function.identity() may be helpful. For example, the following produces a ConcurrentMap mapping students to their grade point average: 
 ConcurrentMap<Student, Double> studentToGPA
   = students.stream().collect(
     toConcurrentMap(Function.identity(),
                     student -> computeGPA(student)));
 ConcurrentMap mapping a unique identifier to students: 
 ConcurrentMap<String, Student> studentIdToStudent
   = students.stream().collect(
     toConcurrentMap(Student::getId,
                     Function.identity()));
 This is a concurrent and unordered Collector.
T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - the mapping function to produce keysvalueMapper - the mapping function to produce valuesCollector 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 elementspublic 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 that accumulates elements into a ConcurrentMap whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided merging function. 
There are no guarantees on the type, mutability, or serializability of the ConcurrentMap returned.
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 deal with these collisions, and produce a ConcurrentMap mapping names to a concatenated list of addresses: 
 ConcurrentMap<String, String> phoneBook
   = people.stream().collect(
     toConcurrentMap(Person::getName,
                     Person::getAddress,
                     (s, a) -> s + ", " + a));
 This is a concurrent and unordered Collector.
T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionkeyMapper - a mapping function to produce keysvalueMapper - a mapping function to produce valuesmergeFunction - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
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 functionpublic 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> mapFactory)
Collector that accumulates elements into a ConcurrentMap whose keys and values are the result of applying the provided mapping functions to the input elements. If the mapped keys contain duplicates (according to Object.equals(Object)), the value mapping function is applied to each equal element, and the results are merged using the provided merging function. The ConcurrentMap is created by a provided supplier function. 
This is a concurrent and unordered Collector.
T - the type of the input elementsK - the output type of the key mapping functionU - the output type of the value mapping functionM - the type of the resulting ConcurrentMap
keyMapper - a mapping function to produce keysvalueMapper - a mapping function to produce valuesmergeFunction - a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)
mapFactory - a supplier providing a new empty ConcurrentMap into which the results will be insertedCollector 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 functionpublic static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
Collector which applies an int-producing mapping function to each input element, and returns summary statistics for the resulting values.T - the type of the input elementsmapper - a mapping function to apply to each elementCollector implementing the summary-statistics reductionpublic static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
Collector which applies an long-producing mapping function to each input element, and returns summary statistics for the resulting values.T - the type of the input elementsmapper - the mapping function to apply to each elementCollector implementing the summary-statistics reductionpublic static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
Collector which applies an double-producing mapping function to each input element, and returns summary statistics for the resulting values.T - the type of the input elementsmapper - a mapping function to apply to each elementCollector implementing the summary-statistics reductionpublic static <T, R1, R2, R> Collector<T,?,R> teeing(Collector<? super T,?,R1> downstream1, Collector<? super T,?,R2> downstream2, BiFunction<? super R1,? super R2,R> merger)
Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result. The resulting collector functions do the following:
The resulting collector is Collector.Characteristics.UNORDERED if both downstream collectors are unordered and Collector.Characteristics.CONCURRENT if both downstream collectors are concurrent.
T - the type of the input elementsR1 - the result type of the first collectorR2 - the result type of the second collectorR - the final result typedownstream1 - the first downstream collectordownstream2 - the second downstream collectormerger - the function which merges two results into the single oneCollector which aggregates the results of two supplied collectors.
    © 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
    https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Collectors.html