How do you use the Collectors utility class in Java?
Table of Contents
- Introduction
- Commonly Used Methods in
Collectors
- Conclusion
Introduction
The **Collectors**
utility class in Java provides a set of predefined methods to help with stream reductions and data aggregation. It is part of the **java.util.stream**
package and is commonly used in Stream API operations to gather, transform, and group data into various types of collections or custom results.
In this guide, we’ll walk through the most commonly used methods of the **Collectors**
class, along with practical examples of how they are applied in real-world Java stream operations.
Commonly Used Methods in Collectors
1. toList()
The **toList()**
method is one of the most frequently used collectors. It collects the elements of a stream into a **List**
. It is especially useful when you want to gather data without any special ordering or grouping.
Example: Collecting Elements into a List
Output:
Here, **toList()**
collects all the elements of the stream into a **List**
.
2. toSet()
Similar to **toList()**
, the **toSet()**
method collects the elements of a stream into a **Set**
. Since a set doesn’t allow duplicates, this collector removes any duplicate elements automatically.
Example: Collecting Elements into a Set
Output:
Here, **toSet()**
removes duplicate elements when collecting them into a **Set**
.
3. joining()
The **joining()**
method is used for concatenating elements in a stream into a single **String**
. You can specify delimiters, prefixes, and suffixes.
Example: Joining Strings with a Delimiter
Output:
In this example, **joining(", ")**
concatenates the strings in the stream with a comma and a space separating them.
4. groupingBy()
The **groupingBy()**
method is a powerful collector that groups elements of a stream by a classifier function (i.e., a function that defines how to classify the elements). It returns a **Map**
, where each key corresponds to a group and each value is a list of elements in that group.
Example: Grouping Elements by Length
Output:
Here, **groupingBy(String::length)**
groups the words based on their length.
5. partitioningBy()
The **partitioningBy()**
method is a specialized form of **groupingBy()**
that splits the stream into two groups based on a predicate. It returns a **Map<Boolean, List<T>>**
, where the key is true
or false
depending on whether the element satisfies the condition.
Example: Partitioning Numbers into Odd and Even
Output:
Here, **partitioningBy(n -> n % 2 == 0)**
partitions the list into two groups: odd numbers and even numbers.
6. counting()
The **counting()**
method counts the number of elements in a stream. It is often used with **groupingBy()**
or **partitioningBy()**
to count the occurrences of different groups.
Example: Counting Occurrences of Elements
Output:
In this example, **groupingBy(word -> word, Collectors.counting())**
groups the words by their value and counts how many times each word appears.
7. mapping()
The **mapping()**
method is used to transform the elements of a stream before they are collected. It is commonly used with **groupingBy()**
to apply a function to each element before grouping.
Example: Mapping Values Before Grouping
Output:
In this example, the **mapping(String::toUpperCase)**
operation is applied to transform each word to uppercase before grouping by its length.
Conclusion
The **Collectors**
utility class in Java provides a wide range of methods for stream aggregation, transformation, and collection. Whether you're collecting elements into a List, Set, or Map, grouping or partitioning data, or performing more complex operations like counting or mapping, the **Collectors**
class is essential for stream-based processing in Java.
By mastering the various collectors available, you can efficiently process and manipulate data within your streams, enabling more readable and powerful code.