KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • map-reduce-filter : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Maps and Sets

var scores = mutableMapOf<String, Int>()
scores["you@illinois.edu"] = 100
scores["me@illinois.edu"] = 100
println(scores)

This lesson introduces two new extremely useful data structures: maps and sets. Together, maps and lists can be used to solve almost any problem. And sets have their uses as well. So let’s get started!

Maps
Maps

Maps represent a fundamentally new data structure for us to consider. So far we’ve looked at data structures that put items in order—like arrays and lists. We’ve also discussed using higher-dimensional arrays when needed to represent higher-dimensional data.

Maps are quite different. They don’t put items in order. Rather, they allow us to store and look up mappings between one thing and other.

More specifically, maps store mappings between keys and values. Each key in a map maps to just one value. However, the same value can be mapped to by multiple keys.

Map Operations
Map Operations

Let’s make this more concrete by looking at a few examples. Just like Kotlin Lists, Maps are built right in and don’t require an imports:

var map = mutableMapOf<String, String>()

Note that, like Lists, Kotlin Maps also utilize type parameters within the angle brackets: <String, String> in the example above. However, Maps require two type parameters: one for the key, and a second for the value. Similar to Lists, Kotlin has both mutable and immutable Maps. We’ll be working primarily with mutable ones.

We can also initialize a Map with some initial values, which allows Kotlin to infer the type of the Map:

var map = mutableMapOf(1 to "one", 2 to "two")
println(map) // inferred to have type parameters <Int, String>

Note the 1 to "one" syntax above, which adds a mapping to the map between 1 (the key) and “one” (the value).

The first map we created above can be used to map Strings to other Strings:

var map = mutableMapOf<String, String>()
map["challen"] = "Geoffrey Challen"
map["colleenl"] = "Colleen Lewis"
println(map)
map["challen"] = "Geoff Challen"
println(map)

We can add mappings to our Map using bracket notation, similar to what we used for modifying arrays and lists! But instead of just integers inside the brackets, now we put the key inside the brackets and the value on the right side of the assignment. So map["challen"] = "Geoff Challen" adds a mapping to the map between “challen” (the key) and “Geoff Challen” (the value). We also show how a second assignment to “challen” overwrites the first, since each key in the map maps to a single value.

var map = mutableMapOf<String, String>()
map["student1"] = "A Student"
map["student2"] = "A Student"
println(map)

In the example above, we show how we can establish two mappings to the same value—both “student1” and “student2” initially map to “A Student”.

var map = mutableMapOf<String, String>()
map["challen"] = "Geoff Challen"
map["colleenl"] = "Colleen Lewis"
println(map["challen"])
println(map["student1"])

To retrieve values from a Map we also use bracket notation and place the key to retrieve inside the brackets. The result is the value for the key, or null if the map does not contain that key. Sometimes we also refer to this as looking up the key in the map: so looking up the mapping for “challen” or “student1” in the example above.

The Elvis Operator
The Elvis Operator

As a brief but useful digression, one of the useful operators that Kotlin provides for working with null safely is called the “null-coalescing” operator. Or, more colorfully, the “Elvis operator”, for reasons that will be obvious shortly.

The Elvis operator evaluates to the left side if it is not null, otherwise to its right side:

var s: String? = null
println(s ?: "right side") // s is null, so ?: evaluates to the right side
s = "left side"
println(s ?: "right side") // s is not null, so ?: evaluates to the left side

The Elvis operator is particularly useful when using Kotlin Maps, as we’ll show in the following example, since it allows us to set a default value to use when a map does not contain a particular key.

Map Example
Map Example

Maps are great for solving problems where we need to save and look up information based on a key. Let’s look at an example that may hit close to home: Recording scores on a homework problem!

var scores = """
you@illinois.edu,0
you@illinois.edu,9
me@illinois.edu,0
you@illinois.edu,10
you@illinois.edu,1
me@illinois.edu,1
me@illinois.edu,0
you@illinois.edu,0"""
fun computeScores(input: String): Map<String, Integer> {
var map = mutableMapOf<String, Integer>()
// Parse the CSV containing scores
for (line in input.trim().split("\n")) {
var parts = line.split(",")
var email = parts[0]
var score = parts[1].toInt()
println("$email -> $score")
}
return map
}
println(computeScores(scores))

Map Iteration
Map Iteration

If you want to iterate over all of the mappings in a Kotlin Map, there are a few different ways to do that:

var map = mutableMapOf(1 to "one", 2 to "two", 4 to "four")
for (entry in map) {
// each entry has a .key and a .value property
println(entry.key)
println(entry.value)
}
// new syntax that we haven't discussed yet
for ((key, value) in map) {
println(key)
println(value)
}
// map.keys will be a list of all the map keys, similar to .indices
for (key in map.keys) {
println(key)
println(map[key])
}

Practice: Word Count With Map

Created By: Geoffrey Challen
/ Version: 2021.9.0

Given a String containing words separated by the "_" character, write a method wordCount that returns a Map<String, Int> containing the number of times that each part appears in the String.

So, for example, given the String "Xyz_Chuchu_Chuchu_Xyz_Ferdie", you would return a map with three mappings: "Xyz" to 2, "Chuchu" to 2, and "Ferdie" to 1.

Sets
Sets

Before we wrap up, let’s briefly examine one other potentially-useful data structure: sets. A set represents an unordered collection of distinct elements. We can add and remove items from a set, but the set either contains the item or does not, which we can test using .contains. Items in a set don’t have an index and are not ordered or counted.

// set initialization
var set = mutableSetOf<Int>()
// adding items
set.add(1)
println(set)
set += 1 // same as set.add(1)
println(set)
set += 2
println(set)
// membership testing
println(set.contains(2))
// set iteration
for (item in set) {
println(item)
}
// item removal
set.remove(1)
println(set)
set -= 2 // same as set.remove(2)
println(set)

Sets are generally less useful that lists or maps. But they do come in hand sometimes, particularly when you need to record membership but don’t care about counts or ordering. Let’s look at an example where a set might come in handy:

var attendance = """
Kermit
Gaffer
Gonzo
gonzo
Gonzo
Scooter
Waldorf
GONZO
Fozzie
Gaffer"""

Practice: Word Lengths With Map

Created By: Geoffrey Challen
/ Version: 2021.9.0

Given a String? containing words separated by the " " (space) character, write a method wordLengths that return a Map<String, Int> mapping each word that is contained in the String to its length. require that the passed String? is not null.

So, for example, given the String "Wow that is amazing", you would return a map with four mappings: "Wow" to 3, "that" to 4, "is" to 2, and "amazing" to 7.

Note that you should not need any import statements, since Lists are built-in to Kotlin and always available.

Homework: Count Repeated Words With Map

Created By: Geoffrey Challen
/ Version: 2022.9.0

Declare and complete a method named countRepeatedWords that, given a List<String>, returns a count of how many Strings appear more than once in the List as an Int. You should complete this problem with a Map, and not use a nested loop.

For example, given the list {"one", "two", "four"}, you would return 0, since no words appear more than once. Given the list {"two", "two", "five"}, you would return 1.

More Practice

Need more practice? Head over to the practice page.