Chapters

Hide chapters

Kotlin Apprentice

Second Edition · Android 10 · Kotlin 1.3 · IDEA

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section III: Building Your Own Types

Section 3: 8 chapters
Show chapters Hide chapters

Section IV: Intermediate Topics

Section 4: 9 chapters
Show chapters Hide chapters

9. Maps & Sets
Written by Eli Ganim & Joe Howard

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

A map is an unordered collection of pairs, where each pair is comprised of a key and a value.

As shown in the diagram above, keys are unique. The same key can’t appear twice in a map, but different keys may point to the same value. All keys have to be of the same type, and all values have to be of the same type.

Maps are useful when you want to look up values by means of an identifier. For example, the table of contents of this book maps chapter names to their page numbers, making it easy to skip to the chapter you want to read.

How is this different from an array? With an array, you can only fetch a value by its index, which has to be an integer, and all indexes have to be sequential. In a map, the keys can be of any type and are generally in no particular order.

Creating maps

The easiest way to create a map is by using the standard library mapOf() function. This function takes a list of Kotlin Pair objects separated by commas:

var yearOfBirth = mapOf("Anna" to 1990, "Brian" to 1991, "Craig" to 1992, "Donna" to 1993)

The Kotlin Pair objects are created using the infix to function. Note that Map<K, V> is an interface, which you’ll learn more about later on. The concrete type that is created depends on which standard library function is called. The mapOf() function returns an immutable map of fixed size.

For your card game from an earlier chapter, instead of using the two arrays to map players to their scores, you can use a map:

var namesAndScores = mutableMapOf("Anna" to 2, "Brian" to 2, "Craig" to 8, "Donna" to 6)
println(namesAndScores) // > {Anna=2, Brian=2, Craig=8, Donna=6}

In this example, the type of the map is inferred to be MutableMap<String, Int>. This means namesAndScores is a map with strings as keys and integers as values, that is, a map from strings to integers.

When you print the map, you see there’s generally no particular order to the pairs. Remember that, unlike arrays, maps are not guaranteed to be ordered!

You can pass in no arguments to the standard library functions to create an empty map like so:

namesAndScores = mutableMapOf()

…or create a new empty map by calling a map constructor:

var pairs = HashMap<String, Int>()

Specifying the type on the variable is not required here, since the compiler can infer the type from the constructor being called.

When you create a map, you can define its capacity:

pairs = HashMap<String, Int>(20)

This is an easy way to improve performance when you have an idea of how much data the map needs to store.

Accessing values

As with arrays, there are several ways to access map values.

Using the index operator

Maps support using square brackets to access values. Unlike arrays, you don’t access a value by its index but rather by its key. For example, if you want to get Anna’s score, you would type:

namesAndScores = mutableMapOf("Anna" to 2, "Brian" to 2, "Craig" to 8, "Donna" to 6)
// Restore the values

println(namesAndScores["Anna"])
// > 2
namesAndScores["Greg"] // null

Using properties and methods

In addition to using indexing, you can also use the get() function to access a value:

  println(namesAndScores.get("Craig"))
  // > 8
namesAndScores.isEmpty() // false
namesAndScores.size      // 4

Modifying mutable maps

It’s easy enough to create maps and access their contents — but what about modifying them? You’ll need a mutable map to do so.

Adding pairs

Bob wants to join the game.

val bobData = mutableMapOf(
  "name" to "Bob",
  "profession" to "CardPlayer",
  "country" to "USA")

bobData.put("state", "CA")
bobData["city"] = "San Francisco"

Mini-exercise

Write a function that prints a given player’s city and state.

Updating values

It appears that in the past, Bob was caught cheating when playing cards. He’s not just a professional — he’s a card shark! He asks you to change his name and profession so no one will recognize him.

bobData.put("name", "Bobby") // Bob
bobData["profession"] = "Mailman"
val pair = "nickname" to "Bobby D"
bobData += pair

println(bobData)
// > {name=Bobby, profession=Mailman, country=USA, state=CA, city=San Francisco, nickname=Bobby D}

Removing pairs

Bob — er, sorry — Bobby, still doesn’t feel safe, and he wants you to remove all information about his whereabouts:

bobData.remove("city")
bobData.remove("state", "CA")

Iterating through maps

The for-in loop works when you want to iterate over a map. But since the items in a map are pairs, you need to use a destructuring declaration:

for ((player, score) in namesAndScores) {
  println ("$player - $score")
}
// > Anna - 2
// > Brian - 2
// > Craig - 8
// > Donna - 6
for (player in namesAndScores.keys) {
  print("$player, ") // no newline
}
println() // print a newline
// > Anna, Brian, Craig, Donna,

Running time for map operations

In order to be able to examine how maps work, you need to understand what hashing is and how it works. Hashing is the process of transforming a value — String, Int, Double, Boolean, etc — to a numeric value, known as the hash value. This value can then be used to quickly look up the values in a hash table.

println("some string".hashCode())
// > 1395333309

println(1.hashCode())
// > 1
println(false.hashCode())
// > 1237

Key points

  • A map is an unordered collection of key-value pairs.
  • The keys of a map are all of the same type, and the values are all of the same type.
  • Use indexing to get values and to add, update or remove pairs.
  • If a key is not in a map, lookup returns null.
  • Built-in Kotlin types such as String, Int, Double have efficient hash values out of the box.
  • Use HashMap<K, V> for performance critical code.

Sets

A set is an unordered collection of unique values of the same type. This can be extremely useful when you want to ensure that an item doesn’t appear more than once in your collection, and when the order of your items isn’t important.

Creating sets

You can declare a set explicitly by using the standard library setOf() function:

val names = setOf("Anna", "Brian", "Craig", "Anna")
println(names)
// > [Anna, Brian, Craig]
val hashSet = HashSet<Int>()

Set from arrays

Sets can be created from arrays. Consider this example:

val someArray = arrayOf(1, 2, 3, 1)
var someSet = mutableSetOf(*someArray)
println(someSet) // > [1, 2, 3]

Accessing elements

You can use contains() to check for the existence of a specific element:

println(someSet.contains(1))
// > true
println(4 in someSet)
// > false

Adding and removing elements

You can use add() to add elements to a set. If the element already exists, the method does nothing.

someSet.add(5)
val removedOne = someSet.remove(1)
println(removedOne) // > true

println(someSet)
// > [2, 3, 5]

Running time for set operations

Sets have a very similar implementations to those of maps, and they also require the elements to have hash values. The HashSet running time of all the operations is identical to those of a HashMap.

Challenges

Check out the following challenges to test your knowledge of maps and sets.

1. val map1: Map<Int to Int> = emptyMap()
2. val map2 = emptyMap()
3. val map3: Map<Int, Int> = emptyMap()
 val map4 = mapOf("One" to 1, "Two" to 2, "Three" to 3)
 4. map4[1]
 5. map4["One"]
 6. map4["Zero"] = 0
 7. map4[0] = "Zero"
 val map5 = mutableMapOf("NY" to "New York", "CA" to "California")
 8. map5["NY"]
 9. map5["WA"] = "Washington"
 10. map5["CA"] = null
fun mergeMaps(map1: Map<String, String>, map2: Map<String, String>): Map<String, String>
fun occurrencesOfCharacters(text: String): Map<Char, Int>
fun isInvertible(map: Map<String, Int>): Boolean
val nameTitleLookup: Map<String, String?>
   = mutableMapOf("Mary" to "Engineer", "Patrick" to "Intern", "Ray" to "Hacker")

Key points

  • Sets are unordered collections of unique values of the same type.
  • Sets are most useful when you need to know whether something is included in the collection or not.

Where to go from here?

Now that you’ve learned about collection types in Kotlin, you should have a good idea of what they can do and when you should use them. You’ll see them come up as you continue on in the book.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now