Unlike many language, Kotlin distinguishes between mutable and immutable collections. Precise control over exactly when collections can be edited is useful for eliminating bugs, and designing good APIs.
1. Using list
1 2 3 4 5 6 7
//Create a new read-only List<String> val list = listOf("Item1", "Item2", "Item3") list.add("Item4") // compile error
//Create a new editable List<String> val list2 = mutableListOf("Item1", "Item2", "Item3") list2.add("Item4")
2. Using map
1 2 3 4 5
//Read-only val map = mapOf(Pair(1, "Item1"), Pair(2, "Item2"), Pair(3, "Item3"))
//Editable val map2 = mutableMapOf(Pair(1, "Item1"), Pair(2, "Item2"), Pair(3, "Item3"))
3. Using set
1 2 3 4 5
//Read-only valset = setOf(1, 3, 5)
//Editable val set2 = mutableSetOf(1, 3, 5)
.
5. Collections
Unlike many language, Kotlin distinguishes between mutable and immutable collections. Precise control over exactly when collections can be edited is useful for eliminating bugs, and designing good APIs.
1. Using list
1 2 3 4 5 6 7
//Create a new read-only List<String> val list = listOf("Item1", "Item2", "Item3") list.add("Item4") // compile error
//Create a new editable List<String> val list2 = mutableListOf("Item1", "Item2", "Item3") list2.add("Item4")
2. Using map
1 2 3 4 5
//Read-only val map = mapOf(Pair(1, "Item1"), Pair(2, "Item2"), Pair(3, "Item3"))
//Editable val map2 = mutableMapOf(Pair(1, "Item1"), Pair(2, "Item2"), Pair(3, "Item3"))