Java Collectors and Its 20 Methods

Rohan Aggarwal
Oct 22 · 6 min read

Collectors is a final class that extends Object class. It provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc. Java Collectors class provides various methods to deal with elements.

Image for post
Image for post

1. Creating list: toList()

It is used to accumulate elements into a list. It will create a new list (It will not change the current list).

2. Creating set: toSet()

It is used to accumulate elements into a set, It will remove all the duplicate entries.

3. Creating specific collection: toCollection()

We can accumulate data in any specific collection as well.

Here we are accumulating elements in a linked list.

4. Counting elements: Counting()

It will return the number of elements present in the computed collection.

5. Finding minimum value: minBy()

It will return the minimum value present in a list.

It will return 1 and alpha, as per the natural order of integers and string.

We can reverse the order using reverseOrder() method.

We can have custom comparator for the user defined objects.

6. Finding maximum value: maxBy()

It is similar to the minimum value method, here we use maxBy() method to get the maximum value.

All the comparator logic which were there in minBy() also apply to maxBy().

7. Partitioning a list: partitioningBy()

It is used for partitioning a list in 2 lists and adding it in the map, 1 that satisfies a given condition and another which does not satisfy like separating odd numbers from the list. So it will make 2 entries in the map, 1 with true as key and odd numbers as value and 2nd with false as key and even numbers as value.

Here we are separating the strings with length greater than 2 from the remaining strings.

8. Creating unmodifiable list: toUnmodifiableList()

It is used to create a read-only list. Any try to make a change in this unmodifiable list will result in UnsupportedOperationException.

9. Creating unmodifiable set: toUnmodifiableSet()

It is used to create a read-only set. Any try to make a change in this unmodifiable set will result in UnsupportedOperationException.

10. Joining elements: joining()

Collectors can be used to create a string by joining all the elements of a collection, with or without any delimiter, suffix and prefix.

In the first example we are joining string by passing a delimiter value (“,”) and in second we are passing the value of prefix and suffix.

11. Averaging long: averagingLong()

Finds the average value of a collection of long values.

NOTE : It will return a Double value not a long value.

12. Averaging int: averagingInt()

Finds the average value of a collection of integer values.

NOTE : It will also return a Double value not an int value.

13. Averaging Double: averagingDouble()

Finds the average value of a collection of double values.

14. Creating Map: toMap()

Creates a map from the values of a collection.

Here we created a map with list values as keys and the number of occurrences of the value in the list as value.

15. Handling duplicates entries of list while creating map

A list can contain duplicate values, so If we want to create a map out of the list and want to use the list values as keys of map then we need to resolve the duplicate keys. As a map only contains unique keys, we can use comparators to do that.

Here, Function.identity() is pointing to the list value and i1, i2 are the values of the duplicate keys. We can keep a single value so, here we are selecting i1 but we can compute anything using these two values like adding them, comparing and selection the greater one etc.

16. Summing Integers: summingInt()

Finding the sum of all the integer values present in the list. It’s not always the sum of the initial collection, which we are using like in the below example we are using the list of strings and first we are converting each string into an integer that is equal to its length and then adding all the lengths.

or direct list value sum

17. Summing Doubles: summingDouble()

Similar to summing integers, just it is used for double values

18. Summing Long: summingLong()

Same as the first two, used for adding long or int values. We can use summinglong() for int values as well, but we cannot use summingInt() for long values. It is based on the concept of typecasting, which data type can be cast in which one.

19. Summarizing Integer: summarizingInt()

It gives all the major arithmetic operation values of the values present in the collection like average of all values, minimum value, maximum values, count and sum of all values.

Now we can extract different values using get methods like:

20. GroupingBy method: groupingBy()

GroupingBy() is a advance method to create a map out of any other collection. It itself is a very vast topic which we will cover in the next blog. SO here just for the completion lets see a few examples

It will make the string length as key and a list of strings of that length as the value.

Here we specified the type of list we want in the map (linked list).

For more information about the Collectors class please refer to the official documentation :

Medium's largest active publication, followed by +732K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store