Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

kotlin

I have a method (getContacts) that returns a List<Contact> and I need to convert this result to a MutableList<Contact>. 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?

like image 475
Mateus Gondim Avatar asked Nov 18 '16 17:11

Mateus Gondim


1 Answers

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.

like image 84
hotkey Avatar answered Sep 18 '22 16:09

hotkey