Reviewing Kotlin data structures
There are three important groups of data structures we should become familiar with in Kotlin: lists, sets, and maps. We’ll provide a brief overview of each here, and then delve into topics related to data structures, including mutability and tuples, in more detail in Chapter 5, Introducing Functional Programming.
Lists
A list in Kotlin represents an ordered collection of elements of the same type. To declare a list, we utilize the listOf()
function instead of calling the constructor of a specific list implementation. This function provides a convenient way to create a list and initialize it with elements:
val hobbits = listOf("Frodo", "Sam", "Pippin", "Merry")
It is important to note that we didn’t explicitly specify the type of the list. This is because type inference can be employed when constructing collections in Kotlin, similar to initializing variables.
If you prefer to...