public static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream,
Function<R,RR> finisher)
The method Collectors#collectingAndThen collects each element per provided downStream collector and converts the type R (which returned from the downstream collector) to a final resultant type RR.
package com.logicbig.example.collectors;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectingAndThenExample {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
List<String> synchronizedList = s.collect(Collectors.collectingAndThen(
Collectors.toList(), Collections::synchronizedList));
System.out.println(synchronizedList);
}
}
Output
[apple, banana, orange]
Original Post