https://kotlinlang.org logo
Title
t

tjohnn

08/01/2019, 10:53 AM
Is there no overhead calling
toMutableList()
again after mapping?
m

marstran

08/01/2019, 11:03 AM
Depends on the actual type of list. If the list is already mutable, then there's no overhead. If it's not, then it copies all elements to a mutable list.
t

tjohnn

08/01/2019, 11:05 AM
Okay I see
s

spand

08/01/2019, 11:27 AM
Collection<T>.toMutableList()
always copies
If you want to end up with something else than a
List
then I suggest you use
mapTo()
m

marstran

08/01/2019, 11:37 AM
Oh, it does? That's weird.
k

karelpeeters

08/01/2019, 12:07 PM
Because you can use a
MutableList
as a
List
it would be pretty weird if it only sometimes copied.
m

marstran

08/01/2019, 12:15 PM
Yeah, nevermind. The not-copying optimization would only make sense for immutable lists.
t

tjohnn

08/01/2019, 12:33 PM
Hmm.. why I wanted to avoid it earlier. Thanks.