Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Structures  ›  Lesson

Map: Looking Up by Key

Map 12 minA Map stores values under unique keys; looking up a missing key with m(key) throws, so get and getOrElse are the safe ways to ask.

Map: Looking Up by Key

At the end of the week the coach asks, "How far did Chen run?" Searching a list of name and distance pairs one by one is slow and clumsy. A Map files each value under a key, so the answer is one lookup away.

The rule

Keys are unique; ask with get or getOrElse when a key might be missing.

val weekKm = Map("Asha" -> 26.7, "Ben" -> 18.0, "Chen" -> 31.4)

weekKm("Chen")                  // 31.4
weekKm.get("Chen")              // Some(31.4)
weekKm.get("Dara")              // None
weekKm.getOrElse("Dara", 0.0)   // 0.0
weekKm("Dara")                  // throws NoSuchElementException
  • "Asha" -> 26.7 makes a pair, the same as ("Asha", 26.7). A Map is built from pairs of key and value.
  • Each key appears once. Adding a pair whose key already exists replaces that key's value.
  • m(key) assumes the key is there and throws if it is not. m.get(key) returns an Option: Some(value) when found, None when not. getOrElse supplies a default for the missing case.

The default Map is immutable, so "changes" return a new map:

val withDara = weekKm + ("Dara" -> 12.0)   // four entries
val fixed = weekKm.updated("Ben", 21.0)    // Ben's value replaced
val noChen = weekKm - "Chen"               // two entries

Other handy parts: weekKm.contains("Asha") is true, weekKm.keys gives the names, and weekKm.values the distances. A for can take each pair apart as it goes:

for (name, km) <- weekKm do println(s"$name: $km km")

Like a Set, a Map does not promise any particular order.

The shortcut

Before any lookup, ask "can this key be missing?" If the key came from the map itself, for example from weekKm.keys, then m(key) is fine. If it came from a person, a form or another list, use getOrElse with a sensible default, or get when the absence itself matters.

Showing Dara's weekly totalStep 1 of 4
  1. Where does the key come from?

    "Dara" was typed by the coach, and Dara has not logged a run yet.

Drills

Answer fast — trust the rule.

quick drill

What is weekKm("Chen")?

quick drill

What is weekKm.get("Dara")?

quick drill

What is (weekKm + ("Asha" -> 30.0)).size?

quick drill

What does weekKm("Dara") do?

quick drill

What is "Asha" -> 26.7?

Remember

  • A Map files values under unique keys, and -> builds a key and value pair.
  • Ask "can this key be missing?" If so, use getOrElse or get.
  • Trap: m(key) throws on a missing key, and adding an existing key replaces its value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainLooking up a member who has not run

The weekly totals are Map("Asha" -> 26.7, "Ben" -> 18.0, "Chen" -> 31.4). The coach types a name to see that runner's total. Explain what weekKm(name), weekKm.get(name) and weekKm.getOrElse(name, 0.0) do for Chen and for Dara, and which one the lookup screen should use.