Learn Kotlin_ Collections Cheatsheet
Learn Kotlin_ Collections Cheatsheet
Collections
Immutable Maps
An immutable Map represents a collection of entries that var averageTemp = mapOf("winter" to 35,
cannot be altered throughout a program.
"spring" to 60, "summer" to 85, "fall" to
It is declared with the term, mapOf , followed by a pair
of parentheses. Within the parentheses, each key should 55)
be linked to its corresponding value with the to keyword,
and each entry should be separated by a comma.
Mutable Maps
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-collections/cheatsheet 1/5
12/12/23, 12:37 PM Learn Kotlin: Collections Cheatsheet | Codecademy
Keys and values within a map can be retrieved using the var oscarWinners = mutableMapOf("Parasite"
.keys and .values properties.
to "Bong Joon-ho", "Green Book" to "Jim
The .keys property returns a list of key elements,
whereas the .values property returns a list of value Burke", "The Shape Of Water" to "Guillermo
elements. del Toro")
To retrieve a single value associated with a key, the
shorthand, [key] , syntax can be used.
println(oscarWinners.keys)
// Prints: [Parasite, Green Book, The
Shape Of Water]
println(oscarWinners.values)
// Prints: [Bong Joon-ho, Jim Burke,
Guillermo del Toro]
println(oscarWinners["Parasite"])
// Prints: Bong Joon-ho
An entry can be added to a mutable map using the put() var worldCapitals = mutableMapOf("United
function. Oppositely, an entry can be removed from a
States" to "Washington D.C.", "Germany" to
mutable map using the remove() function.
The put() function accepts a key and a value separated "Berlin", "Mexico" to "Mexico City",
by a comma. "France" to "Paris")
The remove() function accepts a key and removes the
entry associated with that key.
worldCapitals.put("Brazil", "Brasilia")
println(worldCapitals)
// Prints: {United States=Washington D.C.,
Germany=Berlin, Mexico=Mexico City,
France=Paris, Brazil=Brasilia}
worldCapitals.remove("Germany")
println(worldCapitals)
// Prints: {United States=Washington D.C.,
Mexico=Mexico City, France=Paris,
Brazil=Brasilia}
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-collections/cheatsheet 2/5
12/12/23, 12:37 PM Learn Kotlin: Collections Cheatsheet | Codecademy
Immutable Lists
Mutable Lists
In order to retrieve an element from a list, we can var cars = listOf("BMW", "Ferrari",
reference its numerical position or index using square
"Volvo", "Tesla")
bracket notation.
Note: Remember that the first element of a list starts at
0. println(cars[2]) // Prints: Volvo
The size property is used to determine the number of var worldContinents = listOf("Asia",
elements that exist in a collection.
"Africa", "North America", "South
America", "Antarctica", "Europe",
"Australia")
println(worldContinents.size) // Prints: 7
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-collections/cheatsheet 3/5
12/12/23, 12:37 PM Learn Kotlin: Collections Cheatsheet | Codecademy
List Operations
The list collection supports various operations in the var seas = listOf("Black Sea", "Caribbean
form of built-in functions that can be performed on its
Sea", "North Sea")
elements.
Some functions perform read and write operations, println(seas.contains("North Sea")) //
whereas others perform read-only operations. Prints: true
The functions that perform read and write operations can
only be used on mutable lists while read-only operations
can be performed on both mutable and immutable lists. // The contains() function performs a read
operation on any list and determines if an
element exists.
Immutable Sets
Mutable Sets
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-collections/cheatsheet 4/5
12/12/23, 12:37 PM Learn Kotlin: Collections Cheatsheet | Codecademy
Elements in a set can be accessed using the var companies = setOf("Facebook", "Apple",
elementAt() or elementAtOrNull() functions.
"Netflix", "Google")
The elementAt() function gets appended onto a set
name and returns the element at the specified position
within the parentheses. println(companies.elementAt(3)) // Prints:
The elementAtOrNull() function is a safer variation of
Google
the elementAt() function and returns null if the
position is out of bounds as opposed to throwing an error.
println(companies.elementAt(4)) // Returns
and Error
println(companies.elementAtOrNull(4)) //
Prints: null
Print Share
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-collections/cheatsheet 5/5