Collectors collectingAndThen() method in Java
This post will discuss the collectingAndThen() method provided by the Collectors class in Java, which adapts a collector to perform an additional finishing transformation.
The collectingAndThen(downstream, finisher) method returns a Collector that performs the action of the downstream collector, followed by an additional finishing step with the help of the finisher Function. It is most commonly used for creating immutable Collections.
1. Creating an immutable empty collection
⮚ Create an immutable empty List
We could adapt the Collectors.toList() collector to always produce an immutable empty List, as shown below:
|
1 2 3 |
List<String> immutableList = Stream.of(new String[] {}) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::<String>unmodifiableList)); |
⮚ Create an immutable empty Set
We could adapt the Collectors.toSet() collector to always produce an immutable empty Set, as shown below:
|
1 2 3 |
Set<String> immutableSet = Stream.of(new String[] {}) .collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::<String>unmodifiableSet)); |
⮚ Create an immutable empty Map
We could adapt the Collectors.toMap() collector to always produce an immutable empty Map, as shown below:
|
1 2 3 4 5 |
Map<String, String> immutableMap = Stream.of(new String[][] {}) .collect(Collectors.collectingAndThen( Collectors.toMap(p -> p[0], p -> p[1]), Collections::<String, String>unmodifiableMap )); |
2. Creating an Immutable collection
⮚ Create an immutable list
We could adapt the toList() collector to always produce an unmodifiable list with:
|
1 2 3 |
List<String> list = Stream.of("USA", "UK", "India") .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::<String>unmodifiableList)); |
⮚ Create an immutable Set
We could adapt the toSet() collector to always produce an unmodifiable set with:
|
1 2 3 |
Set<String> set = Stream.of("USA", "UK", "India") .collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::<String>unmodifiableSet)); |
⮚ Create an immutable Map
We could adapt the toMap() collector to always produce an unmodifiable map with:
|
1 2 3 4 |
Map<String, String> immutableMap = Stream.of(new String[][] { {"USA", "Washington DC"}, {"UK", "London"}, {"India", "New Delhi"} }).collect(Collectors.collectingAndThen(Collectors.toMap(p -> p[0], p -> p[1]), Collections::<String, String>unmodifiableMap)); |
3. Reversing a list
We can also write a collector that collects elements in the reversed order with the help of the collectingAndThen() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; // Java program to reverse a list using `Collectors.collectingAndThen()` method class Main { public static void main(String[] args) { List<String> countries = Arrays.asList("USA", "UK", "India"); List<String> reverse = countries.stream() .collect(Collectors.collectingAndThen(Collectors.toList(), l -> { Collections.reverse(l); return l.stream(); } )).collect(Collectors.toList()); System.out.println(reverse); } } |
Output:
[India, UK, USA]
4. Slice a list
We can also use the collectingAndThen() method to get a slice of a list, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; // Java program to get a slice of a list using `Collectors.collectingAndThen()` class Main { public static void main(String[] args) { Stream<Integer> stream = IntStream.rangeClosed(0, 10) .boxed(); // Get a list containing elements from fromIndex to toIndex int fromIndex = 2; int toIndex = 5; List<Integer> slice = stream.collect(Collectors.collectingAndThen( Collectors.toList(), list -> list.stream() .skip(fromIndex) .limit(toIndex - fromIndex + 1) )).collect(Collectors.toList()); System.out.println(slice); } } |
Output:
[2, 3, 4, 5]
That’s all about the Collectors class collectingAndThen() method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)