Learn Kotlin - Collections Cheatsheet - Codecademy
Learn Kotlin - Collections Cheatsheet - Codecademy
Collections
Immutable Lists
An immutable list represents a group of elements with
read-only operations. var programmingLanguages = listOf("C#",
It can be declared with the term listOf , followed by a "Java", "Kotlin", "Ruby")
pair of parentheses containing elements that are
separated by commas.
Mutable Lists
A mutable list represents a collection of ordered
elements that possess read and write functionalities. var fruits = mutableListOf("Orange",
It can be declared with the term, mutableListOf followed "Apple", "Banana", "Mango")
by a pair of parentheses containing elements that are
separated by commas.
println(worldContinents.size) // Prints: 7
List Operations
The list collection supports various operations in the
form of built-in functions that can be performed on its var seas = listOf("Black Sea", "Caribbean
elements. Sea", "North Sea")
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
// The contains() function performs a read
can be performed on both mutable and immutable lists.
operation on any list and determines if an
element exists.
Immutable Sets
An immutable set represents a collection of unique
elements in an unordered fashion whose contents cannot var primaryColors = setOf("Red", "Blue",
be altered throughout a program. "Yellow")
It is declared with the term, setOf , followed by a pair of
parentheses holding unique values.
Mutable Sets
A mutable set represents a collection of ordered
elements that possess both read and write functionalities. var womenInTech = mutableSetOf("Ada
It is declared with the term, mutableSetOf , followed by a Lovelace", "Grace Hopper", "Radia
pair of parentheses holding unique values. Perlman", "Sister Mary Kenneth Keller")
println(companies.elementAtOrNull(4)) //
Prints: null
Immutable Maps
An immutable Map represents a collection of entries that
cannot be altered throughout a program. var averageTemp = mapOf("winter" to
It is declared with the term, mapOf , followed by a pair of 35, "spring" to 60, "summer" to 85,
parentheses. Within the parentheses, each key should be "fall" to 55)
linked to its corresponding value with the to keyword,
and each entry should be separated by a comma.
Mutable Maps
A mutable map represents a collection of entries that
possess read and write functionalities. Entries can be var europeanDomains
added, removed, or updated in a mutable map. = mutableMapOf("Germany" to "de",
A mutable map can be declared with the term, "Slovakia" to "sk", "Hungary" to "hu",
mutableMapOf , followed by a pair of parentheses holding "Norway" to "no")
key-value pairs.
println(oscarWinners.values)
// Prints: [Bong Joon-ho, Jim Burke,
Guillermo del Toro]
println(oscarWinners["Parasite"])
// Prints: Bong Joon-ho
Adding and Removing Map Entries
An entry can be added to a mutable map using the
put() function. Oppositely, an entry can be removed var worldCapitals = mutableMapOf("United
from a mutable map using the remove() function. States" to "Washington D.C.", "Germany" to
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}