Skip to content
DeveloperMemos

Using Kotlin's listOf, setOf and mapOf Functions

Kotlin, Collections, Functional Programming2 min read

In Kotlin, collections play a vital role in managing and manipulating data. The standard library provides several convenient functions for creating immutable collections efficiently. In this article, we will explore the listOf, setOf, and mapOf functions, which are powerful tools for working with lists, sets, and maps, respectively.

Creating a List with listOf

The listOf function allows us to create an immutable list in a concise manner. Consider the following example:

1val fruits = listOf("apple", "banana", "orange")

Here, we create a list of fruits consisting of three elements: "apple", "banana", and "orange". The type of the fruits variable is inferred as List<String>, indicating that it's a read-only list of strings.

Creating a Set with setOf

Similarly, the setOf function enables us to create an immutable set effortlessly. Let's illustrate this with an example:

1val colors = setOf("red", "green", "blue")

In this case, we create a set of colors containing three distinct elements: "red", "green", and "blue". As with listOf, the type of the colors variable is inferred as Set<String>.

Creating a Map with mapOf

The mapOf function allows us to create an immutable map in a straightforward manner. Consider the following example:

1val user = mapOf("name" to "John", "age" to 25)

In this example, we create a map representing a user with two key-value pairs: "name" mapped to "John" and "age" mapped to 25. The type of the user variable is inferred as Map<String, Any>, where the keys are strings, and the values can be of any type.

Accessing Elements

Once we have created our collections, we can access their elements using various approaches provided by Kotlin. For instance, to retrieve an element from a list or set, we can use the indexing operator as follows:

1val firstFruit = fruits[0]
2println(firstFruit) // Output: apple
3
4val color = colors.elementAt(1)
5println(color) // Output: green

In these examples, we access the first element of the fruits list and the element at index 1 of the colors set. Both operations return the desired value.

When working with maps, we can access the values associated with specific keys as demonstrated below:

1val userName = user["name"]
2println(userName) // Output: John
3
4val age = user.getOrDefault("age", 0)
5println(age) // Output: 25

Here, we retrieve the values corresponding to the "name" and "age" keys from the user map. In the second example, we use the getOrDefault function to handle cases where the key is not present in the map.

Modifying Collections

Since the collections created with listOf, setOf, and mapOf are immutable, we cannot modify them directly. However, if we need to make changes or perform transformations, Kotlin provides various functions for doing so.

To create a modified list based on an existing one, we can use functions like map and filter. Here's an example:

1val upperCaseFruits = fruits.map { it.toUpperCase() }
2println(upperCaseFruits) // Output: [APPLE, BANANA, ORANGE]
3
4val filteredColors = colors.filter { it.startsWith("r") }
5println(filteredColors) // Output: [red]

In this snippet, we convert the fruits list to uppercase using the map function and filter the colors set to contain only elements starting with "r" using the filter function.

Conclusion

Kotlin's listOf, setOf, and mapOf functions provide convenient ways of creating immutable collections. By utilizing these functions, you can easily create read-only lists, sets, or maps in your Android development projects. Furthermore, with the wealth of operations available for working with collections in Kotlin, you can efficiently manipulate and transform data as needed.