Skip to content

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 a List. The actual implementation type is ArrayList^[600-developer__java__java8__java8.md#L100].
  • Collectors.toSet(): Accumulates elements into a Set. The actual implementation type is HashSet^[600-developer__java__java8__java8.md#L102].
  • Collectors.toMap(keyMapper, valueMapper): Transforms a Stream into a Map (implementation type HashMap)^[600-developer__java__java8__java8.md#L104].
  • Collectors.toConcurrentMap(...): Transforms a Stream into a ConcurrentHashMap^[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, and averagingDouble calculate the arithmetic mean of numeric values^[600-developer__java__java8__java8.md#L78].
  • Summing: summingInt, summingLong, and summingDouble calculate 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: maxBy and minBy accept a Comparator and return an Optional containing 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 groupingBy as the second argument, one can create a nested Map<Integer, Map<Integer, List<T>>>^[600-developer__java__java8__java8.md#L30-L36].
  • Aggregation: Aggregations like summingInt can be applied to grouped values. For example, grouping items by type and then summing their scores results in a Map<Integer, Integer>^[600-developer__java__java8__java8.md#L38-L42].
  • Reducing: Operations like maxBy or the generic reducing can 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].

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 a Collector to 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].

Sources

^[600-developer__java__java8__java8.md]