Mateus Gondim Mateus Gondim - 21 days ago 5
Android Question

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

I have a method(

getContacts
) that returns a List and I need to convert this result to a MutableList. Currently the best way I can think of doing it is like this:

val contacts: MutableList<Contact> = ArrayList(presenter.getContacts())


Is there a more idiomatic/"less Java" way to do that?

Answer

Consider using the toMutableList() function:

presenter.getContacts().toMutableList()

It is defined as an extension for Collection<T>.