Java Collectors¶
Java Collectors are utility implementations of the Collector interface provided by the java.util.stream package^[600-developer__java__java8__java8.md#L16]. They serve as the primary target for the Stream.collect() terminal operation, used to transform the elements of a Stream into a desired result, such as a Collection, a Map, or a summary calculation^[600-developer__java__java8__java8.md#L16-L121].
Collection Collectors¶
These methods are used to accumulate stream elements into standard Java data structures^[600-developer__java__java8__java8.md#L97-L118].
Collectors.toList(): Accumulates elements into aList. The actual implementation type isArrayList^[600-developer__java__java8__java8.md#L100].Collectors.toSet(): Accumulates elements into aSet. The actual implementation type isHashSet^[600-developer__java__java8__java8.md#L102].Collectors.toMap(keyMapper, valueMapper): Transforms a Stream into aMap(implementation typeHashMap)^[600-developer__java__java8__java8.md#L104].Collectors.toConcurrentMap(...): Transforms a Stream into aConcurrentHashMap^[600-developer__java__java8__java8.md#L106].Collectors.toCollection(supplier): Allows for collection into a user-specified type by passing a supplier constructor reference (e.g.,TreeSet::new)^[600-developer__java__java8__java8.md#L109].
String Joining¶
The joining collector concatenates stream elements into a single String^[600-developer__java__java8__java8.md#L89-L91].
joining(): Joins elements directly.joining(delimiter): Joins elements using the specified delimiter.joining(delimiter, prefix, suffix): Joins elements with a delimiter, and adds a specific prefix and suffix^[600-developer__java__java8__java8.md#L93].
Statistical Reduction¶
These collectors perform mathematical summarization of stream elements^[600-developer__java__java8__java8.md#L78-L96].
- Averaging:
averagingInt,averagingLong, andaveragingDoublecalculate the arithmetic mean of numeric values^[600-developer__java__java8__java8.md#L78]. - Summing:
summingInt,summingLong, andsummingDoublecalculate the sum of numeric values^[600-developer__java__java8__java8.md#L96]. - Counting:
counting()returns the total number of elements in the stream^[600-developer__java__java8__java8.md#L80]. - Min/Max:
maxByandminByaccept aComparatorand return anOptionalcontaining the maximum or minimum element^[600-developer__java__java8__java8.md#L82-L85].
Grouping By¶
The groupingBy collector is used to categorize stream elements based on a classification function^[600-developer__java__java8__java8.md#L16-L66].
Multi-level Collection¶
groupingBy can accept a downstream collector to perform secondary operations on the grouped elements^[600-developer__java__java8__java8.md#L30-L65].
- Secondary Grouping: By passing another
groupingByas the second argument, one can create a nestedMap<Integer, Map<Integer, List<T>>>^[600-developer__java__java8__java8.md#L30-L36]. - Aggregation: Aggregations like
summingIntcan be applied to grouped values. For example, grouping items by type and then summing their scores results in aMap<Integer, Integer>^[600-developer__java__java8__java8.md#L38-L42]. - Reducing: Operations like
maxByor the genericreducingcan be used to reduce grouped elements to a single value^[600-developer__java__java8__java8.md#L44-L61].- Example:
Collectors.groupingBy(x -> x.type, Collectors.reducing(...))^[600-developer__java__java8__java8.md#L58-L61].
- Example:
Composition and Adaption¶
Advanced collectors allow for modifying the stream data or the result before final collection^[600-developer__java__java8__java8.md#L111-L121].
mapping(mapper, downstream): Adapts aCollectorto accept a different type by applying a mapper function to the input elements first^[600-developer__java__java8__java8.md#L116-L118].collectingAndThen(downstream, finisher): Performs a transformation (the finisher function) on the result of the downstream collection^[600-developer__java__java8__java8.md#L112-L114].
Related Concepts¶
- Java Stream API
- [[Java Lambda Expressions]]
- [[Functional Programming]]
Sources¶
^[600-developer__java__java8__java8.md]