collections
val ports = listOf(22, 80, 443)
val mutable = mutableListOf(22, 80, 443)
mutable.add(8080)
Read only versus mutable
listOf returns a read only list
mutableListOf returns a mutable one
The interface does not allow mutation, but the backing instance might
Do not cast a read only list and mutate it
Set and map
val unique = setOf(1, 2, 2, 3)
val m = mapOf("host" to "local", "port" to 443)
setOf removes duplicates
mapOf creates an immutable map
Use mutableMapOf when entries change
Operations
val evens = ports.filter { it % 2 == 0 }
val doubled = ports.map { it * 2 }
The collection functions return new collections They do not mutate the original That is easier to reason about in concurrent code
Next
Coroutines in 08 coroutines
Test this in your lab first
Posted