Kotlin Clone (Shallow) Map or List

Jan 31, 2020

This is shallow clone: copy of the collection structure, not cloning the elements.

List

val items = listOf(1, 2, 3)val newList = items.toList()

or

val newList = items.toMutableList()

or

val newList = ArrayList(items)

Map

val items = mapOf(1 to "Desmond", 2 to "Mei Ru", 3 to "Jack")val newMap = items.toMap()

or

val newMap = items.toMutableMap()

or

val newMap = LinkedHashMap(items)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.