Skip to main content
St Louis

Back to all posts

How to Work With Collections (Lists, Sets, Maps) In Kotlin?

Published on
5 min read
How to Work With Collections (Lists, Sets, Maps) In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
7 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
+
ONE MORE?

Working with collections in Kotlin involves the use of lists, sets, and maps. These data structures allow you to store, retrieve, and manipulate collections of elements.

  1. Lists: A list is an ordered collection of elements. It allows duplicate elements. You can create a list using the listOf function or mutableListOf function for a mutable list. Example: val numbers: List = listOf(1, 2, 3, 4, 5) val mutableNumbers: MutableList = mutableListOf(1, 2, 3, 4, 5)
  2. Sets: A set is an unordered collection of unique elements. It does not allow duplicates. You can create a set using the setOf function or mutableSetOf function for a mutable set. Example: val uniqueNumbers: Set = setOf(1, 2, 3, 4, 5) val mutableUniqueNumbers: MutableSet = mutableSetOf(1, 2, 3, 4, 5)
  3. Maps: A map is a collection of key-value pairs. Each key is unique, and each key maps to a single value. You can create a map using the mapOf function or mutableMapOf function for a mutable map. Example: val employeeMap: Map = mapOf( "John" to "Developer", "Jane" to "Manager", "Mike" to "Designer" ) val mutableEmployeeMap: MutableMap = mutableMapOf( "John" to "Developer", "Jane" to "Manager", "Mike" to "Designer" )
  4. Working with collections: You can perform various operations on collections, such as adding elements, accessing elements, removing elements, and more. For lists and sets, you can use functions like add, add, remove, contains, get, size, etc. For maps, you can use functions like put, remove, containsKey, containsValue, get, size, etc. Example: val numbers: MutableList = mutableListOf(1, 2, 3) numbers.add(4) // Add element to the list numbers.remove(2) // Remove element from the list println(numbers.contains(3)) // Check if list contains an element val employeeMap: MutableMap = mutableMapOf( "John" to "Developer", "Jane" to "Manager" ) employeeMap["Mike"] = "Designer" // Add key-value pair to the map employeeMap.remove("Jane") // Remove key-value pair from the map println(employeeMap.containsKey("John")) // Check if map contains a key

Working with collections in Kotlin provides a convenient way to handle groups of related data. Make sure to review the relevant functions and features provided by Kotlin's collection APIs to effectively utilize and manipulate collections as per your requirements.

How to obtain keys or values from a map in Kotlin?

To obtain keys or values from a map in Kotlin, you can use the keys and values properties of the map.

Here's an example:

val map = mapOf("a" to 1, "b" to 2, "c" to 3)

val keys = map.keys // Obtaining keys from the map val values = map.values // Obtaining values from the map

println(keys) // Prints [a, b, c] println(values) // Prints [1, 2, 3]

In this example, the keys property returns a set of all the keys in the map, while the values property returns a list of all the values in the map. You can also convert these collections to other types, such as List or Array, if needed.

How to check if a list is empty in Kotlin?

There are several ways to check if a list is empty in Kotlin:

  1. Using the isEmpty() function: This function returns true if the list is empty; otherwise, it returns false. Here's an example:

val list = listOf()

if (list.isEmpty()) { println("The list is empty.") } else { println("The list is not empty.") }

  1. Using the isNullOrEmpty() function: This function returns true if the list is null or empty; otherwise, it returns false. Here's an example:

val list: List? = null

if (list.isNullOrEmpty()) { println("The list is null or empty.") } else { println("The list is not null or empty.") }

  1. Checking the size of the list: If the list's size is 0, it means it is empty. Here's an example:

val list = listOf()

if (list.size == 0) { println("The list is empty.") } else { println("The list is not empty.") }

All three approaches will give the same result, so you can choose the one that suits your preference.

What is the difference between mutable and immutable maps in Kotlin?

In Kotlin, mutable maps and immutable maps are two different types of data structures that provide different behaviors for manipulating and accessing data.

  1. Mutable Map: A mutable map, represented by the MutableMap interface, allows modifications to its content. It offers functions like put, remove, clear, etc., which can be used to add, update, remove, or clear elements from the map. Since it is mutable, the map can be modified anytime after its creation.
  2. Immutable Map: An immutable map, represented by the Map interface, does not allow modifications to its content after creation. It offers functions like get, containsKey, keys, etc., which can be used to access and query the map but not modify it. The map, once created, remains fixed and cannot be altered. If you require changes, a new immutable map needs to be created.

The main difference, therefore, lies in the ability to modify the map's contents. Mutable maps can be changed, while immutable maps cannot. Choose the appropriate type based on your use case: if you require the ability to update and modify the map over time, use a mutable map; if you need a fixed, read-only map, use an immutable map.