Sam Stone
04/19/2022, 9:24 PMMutableCollection.addAll(Collection/Iterable/Sequence/Array
) faster if the input and/or output are certain types of collections?ephemient
04/19/2022, 9:37 PMIterable.addAll
method; do you mean MutableCollection.addAll
?ephemient
04/20/2022, 12:55 AMfun <T> MutableCollection<in T>.addAll(elements: Iterable<T>): Boolean
is an extension function which forwards to the
interface MutableCollection<E> {
fun addAll(elements: Collection<E>): Boolean
}
member if elements is Collection
else it adds the elements one-by-one. whether the member function .addAll
does anything better depends on the receiver in questionephemient
04/20/2022, 12:59 AMjava.util.ArrayList
, which is what you'll get if you use mutableListOf()
in Kotlin (in current implementation, not guaranteed by the API contract), does pre-allocate space so it can grow the backing array only once, but this has the same big-O amortized runtimeSam Stone
04/20/2022, 5:06 PMephemient
04/20/2022, 5:08 PMSam Stone
04/20/2022, 5:10 PMephemient
04/20/2022, 5:11 PMephemient
04/20/2022, 5:13 PMlistOf()
and java.util.List.of() are immutable, but you don't know it from the typeSam Stone
04/20/2022, 5:15 PMmutableListOf().addAll(listOf())
runs in linear time (no more efficient than Iterable
), and adding an immutable list would theoretically be O(1), but there aren't any such types...?ephemient
04/20/2022, 5:16 PMSam Stone
04/20/2022, 5:18 PMephemient
04/20/2022, 5:19 PMSam Stone
04/20/2022, 5:19 PMephemient
04/20/2022, 5:19 PMephemient
04/20/2022, 5:19 PM