1.The collect is declared by the Interface of Stream.The param is Collector Interface.
<R, A> R collect(Collector<? super T, A, R> collector);
2.The Collector Interface mainly contains 4 functions about:
(1)creation of a new result container ({@link #supplier()})
(2)incorporating a new data element into a result container ({@link #accumulator()})
(3)combining two result containers into one ({@link #combiner()})
There some differences that sequential and parallel execution by this function.The parallel execution is not always rapider than sequential.You should measure the expenditure of resource that threads switch.
(4)performing an optional final transform on the container ({@link #finisher()}
3.The Collectors Class has a inner class which implements above Collector.So you can invoke collect by this assistant class (Collectors).The Collectors class implement some common operations.
ex:
public static <T> Collector<T, ?, List<T>> toList() { return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID); }
You should know this specific implement.Because of you can get a final ArrarList result by above toList,How can you get a final LinkedList?
浙公网安备 33010602011771号