Kotlin - Most idiomatic way to convert a List to a MutableList
Solution 1:
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<T>
, Iterable<T>
, Sequence<T>
, CharSequence
, Array<T>
and primitive arrays.
Solution 2:
If you only wants ArrayList, you can create your own Kotlin extension.
fun <T> List<T>.toArrayList(): ArrayList<T>{
return ArrayList(this)
}
Then you can use it in your application like
myList.toArrayList()
Simple and easy