I was writing a small piece of code in which I internally handle my data in a mutable map, which in turn has mutable lists.
I wanted to expose my data to the API user, but to avoid any unsafe publication of my data I wanted to expose it in immutable collections even when internally being handled by mutable ones.
class School {
val roster: MutableMap<Int, MutableList<String>> = mutableMapOf<Int, MutableList<String>>()
fun add(name: String, grade: Int): Unit {
val students = roster.getOrPut(grade) { mutableListOf() }
if (!students.contains(name)) {
students.add(name)
}
}
fun sort(): Map<Int, List<String>> {
return db().mapValues { entry -> entry.value.sorted() }
.toSortedMap()
}
fun grade(grade: Int) = db().getOrElse(grade, { listOf() })
fun db(): Map<Int, List<String>> = roster //Uh oh!
}
I managed to expose only Map
and List
(which are immutable) in the public API of my class, but the instances I am actually exposing are still inherently mutable.
Which means an API user could simply cast my returned map as an ImmutableMap and gain access to the precious private data internal to my class, which was intended to be protected of this kind of access.
I couldn't find a copy constructor in the collection factory methods mutableMapOf()
or mutableListOf()
and so I was wondering what is the best and most efficient way to turn a mutable collection into an immutable one.
Any advice or recommendations?
Java offers List. copyOf to convert a mutable list to an immutable one (copying it if needed), but offers no method that inverts this logic (creating a mutable List only if the input isn't mutable already).
Kotlin mutableListOf()MutableList class is used to create mutable lists in which the elements can be added or removed. The method mutableListOf() returns an instance of MutableList Interface and takes the array of a particular type or mixed (depends on the type of MutableList instance) elements or it can be null also.
Use Collections to converts a Mutable list to Immutable list, Example:
Mutable list:
val mutableList = mutableListOf<String>()
Converts to Immutable list:
val immutableList = Collections.unmodifiableList(mutableList)
Currently in Kotlin stdlib there are no implementations of List<T>
(Map<K,V>
) that would not also implement MutableList<T>
(MutableMap<K,V>
). However due to Kotlin's delegation feature the implementations become one liners:
class ImmutableList<T>(private val inner:List<T>) : List<T> by inner
class ImmutableMap<K, V>(private val inner: Map<K, V>) : Map<K, V> by inner
You can also enhance the creation of the immutable counterparts with extension methods:
fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> {
if (this is ImmutableMap<K, V>) {
return this
} else {
return ImmutableMap(this)
}
}
fun <T> List<T>.toImmutableList(): List<T> {
if (this is ImmutableList<T>) {
return this
} else {
return ImmutableList(this)
}
}
The above prevents a caller from modifying the List
(Map
) by casting to a different class. However there are still reasons to create a copy of the original container to prevent subtle issues like ConcurrentModificationException:
class ImmutableList<T> private constructor(private val inner: List<T>) : List<T> by inner {
companion object {
fun <T> create(inner: List<T>) = if (inner is ImmutableList<T>) {
inner
} else {
ImmutableList(inner.toList())
}
}
}
class ImmutableMap<K, V> private constructor(private val inner: Map<K, V>) : Map<K, V> by inner {
companion object {
fun <K, V> create(inner: Map<K, V>) = if (inner is ImmutableMap<K, V>) {
inner
} else {
ImmutableMap(hashMapOf(*inner.toList().toTypedArray()))
}
}
}
fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> = ImmutableMap.create(this)
fun <T> List<T>.toImmutableList(): List<T> = ImmutableList.create(this)
While the above is not hard to implement there are already implementations of immutable lists and maps in both Guava and Eclipse-Collections.
As mentioned here and here, you'd need to write your own List
implementation for that, or use an existing one (Guava's ImmutableList comes to mind, or Eclipse Collections as Andrew suggested).
Kotlin enforces list (im)mutability by interface only. There are no List
implementations that don't also implement MutableList
.
Even the idiomatic listOf(1,2,3)
ends up calling Kotlin's ArraysUtilJVM.asList()
which calls Java's Arrays.asList()
which returns a plain old Java ArrayList
.
If you care more about protecting your own internal list, than about the immutability itself, you can of course copy the entire collection and return it as an List
, just like Kotlin does:
return ArrayList(original)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With