Java 8 Stream.collect() – Stream.Collectors APIs Examples

Java 8 Stream.collect() - Stream.Collectors APIs with Examples


Java 8 provides an extremely powerful abstract concept Stream with many useful mechanics for consuming and processing data in Java Collection. In the tutorial, We will do lots of examples to explore more the helpful of Stream API with Collectors operation on the specific topic: “Java 8 Stream Collector”.

What will we do?

  • Explore Stream Collect Method signatures
  • Apply Stream Collectors Util with Examples

Now let’s do more details!

Related posts:
Java 8 Stream Reduce Examples
Java 8 Stream FlatMap Examples

Java Stream.collector()

Stream.collect(supplier, accumulator, combiner)

– Method Signature:

It performs a mutable reduction operation on the elements of this stream to update the result container.

R is the type of result container
supplier is a function that creates a new mutable result container.
combiner is an associative, non-interfering, stateless function that accepts two partial result containers and merges them.

How does it work?

– Example:

Stream.collect(Collector collector)

– Method Signature:

-> A Collector encapsulates the functions used as arguments to collect(Supplier, BiConsumer, BiConsumer).

R: is the type of the result
A: is the intermediate accumulation type of the Collector

Example:

java.util.stream.Collectors Examples

java.util.stream.Collectors is an implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria.

Collectors.toList(), toSet(), toMap()

Collectors.toList()

– Returns a Collector that accumulates the input elements into a new List.

Collectors.toSet()

– Returns a Collector that accumulates the input elements into a new Set.

Collectors.toMap()

– Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.

Collectors.toCollection()

With Collectors.toCollection(), we can custom the implementation of collection.

-> It returns a Collector that accumulates the input elements into a new Collection.
The Collection is created by the provided factory.

– Example:

Collectors.joining()

It returns a Collector that concatenates the input elements into a String, in encounter order

– Signature 1:

Example:

– Signature 2:

-> It returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter order.

Example:

– Signature 3:

-> Returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix.

Collectors.mapping()

-> Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a mapping function to each input element before accumulation.

mapper is a function to be applied to the input elements
downstream is a collector which will accept mapped values

  • T the type of the input elements
  • U type of elements accepted by downstream collector
  • A intermediate accumulation type of the downstream collector
  • R result type of collector

– Example:

Collectors.filtering()

-> It adapts a 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.

– Example:

Collectors.minBy()/maxBy()

Collectors.minBy()

-> It returns a Collector that produces the minimal element according to a given Comparator.

Collectors.maxBy()

-> It returns a Collector that produces the maximal element according to a given Comparator

Collectors.collectingAndThen()

-> It adapts a Collector to perform an additional finishing transformation.

finisher is a function to be applied to the final result of the downstream collector.

– Example:

Collectors.summingInt()/summingLong()/summingDouble()

-> It returns a Collector that produces the sum of a integer-valued function applied to the input elements.
If no elements are present, the result is 0.

– Example:

We do summingLong()/summingDouble() as the same way with summingInt().

Collectors.averagingInt()/averagingLong()/averagingDouble()

Signature:

-> Returns a 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.

averagingInt() example:

We do averagingLong()/averagingDouble() as the same way with averagingInt().

Collectors.reducing()

-> Returns a Collector which performs a reduction of its input elements under a specified BinaryOperator using the provided identity.

– Example:

Collectors.groupingBy()

-> It returns a 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 collector produces a Map with:
+ Keys are the values resulting from applying the classification function to the input elements
+ Corresponding values are Lists containing the input elements which map to the associated key under the classification function.

– Example:

Collectors.partitioningBy()

-> It returns a Collector which partitions the input elements according to a Predicate, and organizes them into a Map.

– Example:

Collectors.summarizingInt()

-> It returns a Collector which applies an int-producing mapping function to each input element, and returns summary statistics for the resulting values.

We also have summarizingLong(), summarizingDouble() and apply them in programming as the same way doing with summarizingInt.

Conclusion

We had learn how to use Java 8 Stream Collector APIs to perform mutable fold operations:

  • How to use Stream.collect() methods with difference signatures APIs
  • Apply Stream.collect() with many java.util.stream.Collectors()‘s implementation: toList(), toSet(), toMap(), summarizingInt(), partitioningBy(), groupingBy(), …

Happy learning! See you later!

By grokonez | April 17, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*