Single-level grouping
All grouping collectors have a classification function (the function that classifies the elements of the stream into different groups). Mainly, this is an instance of the Function<T, R> functional interface.
Each element of the stream (of the T type) is passed through this function, and the return will be a classifier object (of the R type). All the returned R types represent the keys (K) of a Map<K, V>, and each group is a value in this Map<K, V>.
In other words, the key (K) is the value returned by the classification function, and the value (V) is a list of elements in the stream that have this classified value (K). So, the final result is of the Map<K, List<T>> type.
Let's look at an example to bring some light to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access