Mastering Java 8's Collectors Class
Introduction
Java 8 introduced the Stream API, which revolutionized the way we process collections of data. One of the key components of this API is the Collectors
class, which provides various methods to perform reduction operations on streams. In this blog post, we'll explore the Collectors
class, its methods, and how to use them with practical code examples.
What is the Collectors Class?
The Collectors
class is a final class that extends the Object
class. It provides a variety of static methods that return instances of the Collector
interface. These methods are used to perform mutable reduction operations on streams, such as accumulating elements into collections, summarizing elements according to various criteria, and more.
Common Collectors Methods
Let's dive into some of the most commonly used methods in the Collectors
class:
- toList()
- toSet()
- toMap()
- joining()
- groupingBy()
- partitioningBy()
- summarizingInt(), summarizingDouble(), summarizingLong()
1. Collectors.toList()
The toList()
method collects the elements of a stream into a List
.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
List items = Stream.of("apple", "banana", "orange")
.collect(Collectors.toList());
System.out.println(items); // Output: [apple, banana, orange]
}
}
2. Collectors.toSet()
The toSet()
method collects the elements of a stream into a Set
.
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
Set items = Stream.of("apple", "banana", "orange", "apple")
.collect(Collectors.toSet());
System.out.println(items); // Output: [banana, orange, apple]
}
}
3. Collectors.toMap()
The toMap()
method collects the elements of a stream into a Map
.
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
Map items = Stream.of("apple", "banana", "orange")
.collect(Collectors.toMap(item -> item, item -> item.length()));
System.out.println(items); // Output: {banana=6, orange=6, apple=5}
}
}
4. Collectors.joining()
The joining()
method concatenates the elements of a stream into a single String
.
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
String result = Stream.of("apple", "banana", "orange")
.collect(Collectors.joining(", "));
System.out.println(result); // Output: apple, banana, orange
}
}
5. Collectors.groupingBy()
The groupingBy()
method groups the elements of a stream by a classifier function.
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
Map> groupedByLength = Stream.of("apple", "banana", "orange")
.collect(Collectors.groupingBy(String::length));
System.out.println(groupedByLength); // Output: {5=[apple], 6=[banana, orange]}
}
}
6. Collectors.partitioningBy()
The partitioningBy()
method partitions the elements of a stream into two groups based on a predicate.
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
Map> partitionedByLength = Stream.of("apple", "banana", "orange")
.collect(Collectors.partitioningBy(item -> item.length() > 5));
System.out.println(partitionedByLength); // Output: {false=[apple], true=[banana, orange]}
}
}
7. Collectors.summarizingInt(), summarizingDouble(), summarizingLong()
These methods collect statistics, such as count, sum, min, average, and max, for the elements of a stream.
import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectorsExample {
public static void main(String[] args) {
IntSummaryStatistics stats = Stream.of(1, 2, 3, 4, 5)
.collect(Collectors.summarizingInt(Integer::intValue));
System.out.println(stats); // Output: IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
}
}
Conclusion
The Collectors
class in Java 8 provides a powerful and flexible way to perform reduction operations on streams. By mastering these methods, you can write more concise and readable code. Whether you're collecting elements into a list, grouping them by a certain criterion, or summarizing their statistics, the Collectors
class has you covered.