KotlinCS 124 LogoJava

Practice with Collections

var petNames = mutableMapOf<String, MutableList<String>>()
petNames["Geoffrey"] = mutableListOf()
petNames["Geoffrey"]!! += "Gracie"
petNames["Geoffrey"]!! += "Xyz"
println(petNames)

Let’s pause before moving on to get more practice with Kotlin’s collections—the lists, maps, and sets that are so useful for solving problems. We’ll also learn how we can combine these collections together to build more interesting data structures. Let’s get started!

Nested Collections
Nested Collections

In the past lessons we’ve seen how to create and use several standard Kotlin collections: Lists, Maps, and Sets:

var friends = mutableListOf<String>("you")
friends += "Gracie"
println(friends)
var favoriteIceCreams = mutableMapOf<String, String>("me" to "Peanut Butter Fudge")
favoriteIceCreams["Gracie"] = "Human food yummy"
favoriteIceCreams["Xyz"] = "Yick"
println(favoriteIceCreams)

These collections are quite useful on their own! However, they can also be combined to great effect. Let’s see an example.

// Task list example

Lists of Maps of Sets
Lists of Maps of Sets

You can combine Lists, Maps, and Sets in many interesting ways to build data structures to solve problems. You can create Lists of Maps:

var values = mutableListOf<MutableMap<String, String>>()
var entry = mutableMapOf<String, String>()
entry["test"] = "me"
values += entry
println(values)

Or Sets of Lists:

var first = mutableListOf("test", "me")
var second = mutableListOf<String>()
second += "test"
second += "me"
var set = mutableSetOf<List<String>>()
set += first
println(set)
set += second
// Because first and second have the same items, adding second does not modify the set
println(set)

But generally, it’s more common for the top-level data structure to be a Map: Maps of Maps, Maps of Lists, and Maps of Sets. We’ll get some practice working with these on this lesson’s practice and homework problems.

Kotlin Maps and Nullability
Kotlin Maps and Nullability

One thing you may have noticed is that Kotlin’s null checking doesn’t work so well in some cases on maps. For example:

var map = mutableMapOf("test" to 1)
// This should work, but fails
map["test"] += 1

Let’s briefly discuss why this is, and what we can do about it.

var map = mutableMapOf("test" to 1)
// This should work, but fails
map["test"] += 1

Lists to Map Warm-Up
Lists to Map Warm-Up

We’ll spend the rest of the lesson working on some problems that test our understanding of how to nest collections. First, we’re asked to parse a List<String> into a Map<Set<String>>. Let’s do an example of that together, which you can use as a starting point for the practice problem that follows.

// Warm-up for Section Lists to Map

Homework Problem Warm-Up
Homework Problem Warm-Up

Today’s homework problem is a challenge! Your goal is to complete the implementation of the Hawaiian Word translator you began earlier this semester. Since this problem is more difficult than other homework problems, we’re giving you an extra day to complete it. And, at the end of the day, please remember that this is just one problem, and you have homework drops.

We’ll also get you started with a walkthrough to help you think about how to approach this problem.

Note that the problem asks you to throw an exception in certain cases, something that we have not yet covered. The walkthrough describes how to do that.

// Warm-up for Hawaiian Words

CS People: Dina Katabi
CS People: Dina Katabi

Very few people can make a legitimate claim to the label “genius”. Dina Katabi is one of them. A full professor at MIT, her groundbreaking work on wireless networking and other topics has also earned her a MacArthur Fellowship, the substantial financial award unofficially known as the “Genius Grant”.

In this video she discusses some of her work, including the ability to use wireless signals is a way that you may find quite surprising:

More Practice

Need more practice? Head over to the practice page.