0% found this document useful (0 votes)
78 views

Learn Kotlin - Collections Cheatsheet - Codecademy

This document provides an overview of collections in Kotlin, including lists, sets, and maps. It describes how to declare immutable and mutable versions of each collection type. It also demonstrates how to access and manipulate elements within collections using various functions like size, contains, elementAt, get, put, and remove.

Uploaded by

Diprot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Learn Kotlin - Collections Cheatsheet - Codecademy

This document provides an overview of collections in Kotlin, including lists, sets, and maps. It describes how to declare immutable and mutable versions of each collection type. It also demonstrates how to access and manipulate elements within collections using various functions like size, contains, elementAt, get, put, and remove.

Uploaded by

Diprot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn Kotlin

Collections
Immutable Lists
An immutable list represents a group of elements with
read-only operations. var programmingLanguages
It can be declared with the term listOf , followed = listOf("C#", "Java", "Kotlin",
by a pair of parentheses containing elements that are "Ruby")
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, "Apple", "Banana", "Mango")
mutableListOf followed by a pair of
parentheses containing elements that are separated by
commas.

Accessing List Elements


In order to retrieve an element from a list, we can
reference its numerical position or index using square var cars = listOf("BMW", "Ferrari",
bracket notation. "Volvo", "Tesla")
Note: Remember that the rst element of a list starts at
0. println(cars[2]) // Prints: Volvo

The Size Property


The size property is used to determine the
number of elements that exist in a collection. var worldContinents = listOf("Asia",
"Africa", "North America", "South
America", "Antarctica", "Europe",
"Australia")

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",
elements. "Caribbean 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
// The contains() function performs
operations can be performed on both mutable and
a read operation on any list and
immutable lists.
determines if an element exists.  

seas.add("Baltic Sea") // Error:


Can't perform write operation on
immutable list

// The add() function can only be


called on a mutable list thus the
code above throws an error.

Immutable Sets
An immutable set represents a collection of unique
elements in an unordered fashion whose contents var primaryColors = setOf("Red",
cannot be altered throughout a program. "Blue", "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 var womenInTech = mutableSetOf("Ada
functionalities. Lovelace",  "Grace Hopper",  "Radia
It is declared with the term, mutableSetOf , Perlman",  "Sister Mary Kenneth
followed by a pair of parentheses holding unique Keller")
values.

Accessing Set Elements


Elements in a set can be accessed using the
elementAt() or elementAtOrNull() var companies = setOf("Facebook",
functions. "Apple", "Netflix", "Google")
The elementAt() function gets appended onto
a set name and returns the element at the speci ed println(companies.elementAt(3)) //
position within the parentheses. Prints: Google
The elementAtOrNull() function is a safer
variation of the elementAt() function and println(companies.elementAt(4)) //
returns null if the position is out of bounds as
Returns and Error
opposed to throwing an error.
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 35,  "spring" to 60,  "summer" to 85,
pair of parentheses. Within the parentheses, each key "fall" to 55)
should be 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
mutableMapOf , followed by a pair of "hu", "Norway" to "no")
parentheses holding key-value pairs.

Retrieving Map Keys and Values


Keys and values within a map can be retrieved using the
.keys and .values properties. var oscarWinners
The .keys property returns a list of key elements, = mutableMapOf("Parasite" to "Bong
Joon-ho", "Green Book" to "Jim
whereas the .values property returns a list of
value elements.
Burke", "The Shape Of Water" to
To retrieve a single value associated with a key, the "Guillermo del Toro")
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

/
Adding and Removing Map Entries
An entry can be added to a mutable map using the
put() function. Oppositely, an entry can be var worldCapitals
removed from a mutable map using the remove()
= mutableMapOf("United States" to
function. "Washington D.C.", "Germany" to
The put() function accepts a key and a value
"Berlin", "Mexico" to "Mexico City",
separated 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}

You might also like