Creating a Map object from Stream elements using Collectors.groupingBy() allows for a lot of flexibilit
Articles related to : Java programming
Java streams 31. Collect 7. Collectors.toMap() collector
Creating Map with keys and values based on the elements emitted by a stream is easy, if one uses the ready-to-use collectors from Collectors class. Let’s start with the simplest of map collectors toMap().
Java streams 30. Collect 6. Collectors.toList(), .toSet(), and .toCollection()
Creating List or Set object or any collection from stream elements is easy. Just call collect() operation and pass the corresponding collector, generated by one of the factory methods of the Collectors class.
Java streams 28. Collect 4. Counting stream elements
The collect(Collectors.counting()) is a very straightforward operation. It counts a number of emitted elements. But it does a bit more than the Stream operator count().
Java streams 27. Collect 3. Collectors summing, averaging, and summarizing.
In this post, we will discuss and demonstrate usage of the ready-to-use collectors that calculate some statistics of the stream elements – count, sum, average, min, and max element of the stream.
Java streams 29. Collect 5. String.join() and Collectors.joining()
Sometimes we need to create a string that contains comma-separated values emitted by a stream. The collect() operation with the Collectors.joining() collector (with a parameter that describes a delimiter) is very convenient way to do it.
Java streams 26. Collect 2. The .maxBy() and .minBy() collectors
Collectors.maxBy() and Collectors.minBy() collectors calculate the max and the min element correspondingly according to a given Comparator
Java streams 25. Collect 1. Custom collector
The collect() operation is a specialization of reduce(). It allows implementing a vast variety of algorithms using the ready-to-use implementations of collectors located in the java.util.stream.Collectors class.
Java streams 24. Reduce
The reduce() operation accumulates stream elements into one resulting value, thus “reducing” the stream of values to one value of the same or a different type.
Java streams 23. ToArray
The toArray() operation collects the emitted stream values into an array no matter whether the stream is parallel or not. The only limitation is that the stream has to be finite. Otherwise, the processing never ends.