Effective Kotlin Item 59: Consider using mutable collections
The biggest advantage of using mutable collections instead of immutable collections is that their performance is faster. When we add an element to an immutable collection, we need to create a new collection and add all elements to it. Here is how this is currently implemented in Kotlin stdlib (Kotlin 1.2):
operator fun <T> Iterable<T>.plus(element: T): List<T> {
if (this is Collection) return this.plus(element)
val result = ArrayList<T>()
result.addAll(this)
result.add(element)
return result
}
When we deal with bigger collections, adding multiple elements to another collection can be a costly process. This is why using mutable collections, especially if we often need to add elements, is a performance optimization. On the other hand, Item 1: Limit mutability taught us the advantages of using immutable collections for safety. Notice that these arguments rarely apply to local variables, where synchronization or encapsulation is rarely needed. This is why it generally makes more sense to use mutable collections for local processing. This is reflected in the standard library, where all collection processing functions are internally implemented using mutable collections:
inline fun <T, R> Iterable<T>.map(
transform: (T) -> R
): List<R> {
val size =
if (this is Collection<*>) this.size else 10
val destination = ArrayList<R>(size)
for (item in this)
destination.add(transform(item))
return destination
}
Instead of using immutable collections:
// This is not how map is implemented
inline fun <T, R> Iterable<T>.map(
transform: (T) -> R
): List<R> {
var destination = listOf<R>()
for (item in this)
destination += transform(item)
return destination
}
Adding mutable collections to elements is generally faster, but immutable collections give us more control over how they are changed. However, in the local scope we generally do not need this control, so mutable collections should be preferred, especially in util functions, where element insertion might happen many times.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, is known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.
After working as a Java engineer for 8 years in various French IT companies, I moved to mobile application development on iOS and Android in 2012. In 2015, I decided to focus on Android and joined i-BP, an IT department of the French banking group BPCE, as Android expert. I am now passionate about Android, clean code, and, of course, Kotlin programming.
I am a mobile and web developer with over 10 years of experience. Currently, I’m specializing in mobile development (both native and cross-platform), but I was working as a frontend and backend developer as well, so I know the development process from each side.
I’m working as a Head of Mobile Development at Mews, so creating the architecture, code reviewing, mentoring, and integrating best practices is a part of my everyday job.
Currently, I’m mainly interested in Flutter. We have an application written in Flutter that is successfully running in production for more than a year.
If you need help with defining the right architecture for your mobile app (both from back-end and front-end sides) or looking for a mentor / code reviewer, feel free to contact me in LinkedIn or Twitter and let’s have a talk.