In java sources, Collectors.toList is defined as
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); }
We see BinaryOperator as the third design parameter of CollectorImpl, which combines two subselects in linear time. Does this mean that in the case of frequent use of this function by the Stream.collect method, we can get the square calculation time?
Consider this code:
List<Integer> desc = Stream.iterate(n, k -> k - 1).limit(n + 1) .collect(Collectors.toList()); desc.parallelStream() .map(k -> { try { Thread.sleep(k * 500); } catch (InterruptedException ignored) { } return k; }) .collect(Collectors.toList());
Elements of the second stream were calculated in descending order. The easiest way a collection method can do is to count the number of each number, which transfers it to the list and adds all subsequent numbers to it with total complexity, how sad it is.
source share