Loading...

Kotlin - Most idiomatic way to convert a List to a MutableList

Answer #1 100 %

Consider using the toMutableList() function:

presenter.getContacts().toMutableList()

There are toMutableList() extensions for the stdlib types that one might want to convert to a mutable list: Collection, Iterable, Sequence, CharSequence, Array and primitive arrays.

Answer #2 100 %

If you only wants ArrayList, you can create your own Kotlin extension.

fun  List.toArrayList(): ArrayList{
    return ArrayList(this)
}

Then you can use it in your application like

myList.toArrayList()

Simple and easy

You’ll also like:


© 2023 CodeForDev.com -