Davio
05/30/2024, 7:31 AMtoList
creating a list that needs to be resized a couple of times to accommodate more and more items.
If I'm not mistaken, the implementation for toList
is:
public fun <T> Sequence<T>.toList(): List<T> {
val it = iterator()
if (!it.hasNext())
return emptyList()
val element = it.next()
if (!it.hasNext())
return listOf(element)
val dst = ArrayList<T>()
dst.add(element)
while (it.hasNext()) dst.add(it.next())
return dst
}
Here you can see it creates an ArrayList
and keeps adding the next element.
Would it be useful / make sense to have a specialization of sequence, such as BoundedSequence
where the number of elements is known in advance so it can be carried over to the last operator to avoid creating multiple collections?Klitos Kyriacou
05/30/2024, 8:21 AMsequence.toCollection(ArrayList(size))
.hfhbd
05/30/2024, 10:02 AMDavio
05/30/2024, 10:38 AM