Collections

Kotlin has simple funtions to initialize collections. The following line of code shows an example of initializing a list:

    val countries = listOf("India", "China", "USA")

The following code snippet shows some of the important operations that can be performed on a list:

    println(countries.size)//3    println(countries.first())//India    println(countries.last())//USA    println(countries[2])//USA

Lists, created with listOf, are immutable in Kotlin. To be able to change the content of a list, the mutableListOf function needs to be

    //countries.add("China") //Not allowed    val mutableContries = mutableListOf("India", "China", "USA")    mutableContries.add("China")

The mapOf function is used to initialize a map, as shown in the following code snippet: ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.