When working with Kotlin, you will come across the concept of a read-only view of a mutable collection. You will probably wonder what the difference between this and an immutable collection is.
It is easier to understand with an example. In this case, let's create a mutable list of strings. This applies to all the collections we have covered:
val carManufacturers: MutableList<String> = mutableListOf("Masserati", "Aston Martin","McLaren","Ferrari","Koenigsegg") val carsView: List<String> = carManufacturers carManufacturers.add("Lamborghini") println("Cars View:$carsView") //Cars View: Masserati, Aston Martin, McLaren, Ferrari, Koenigsegg, Lamborghini
The code initializes a mutable list of car manufacturers and then provides a view on it through the...