Problem overview
The function phoneBookLookup receives phoneBook, a map from names to phone numbers, and a name. It returns the phone number stored under that name. The name is always a key of the map, and numbers are strings like "555-0142".
With {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people can share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and "Tim", the answer is "555-0111". Only the key is used to look up the value.
Approach
A map answers "what is stored under this key?" directly, with no loop. In Kotlin you read it with square brackets: phoneBook[name].
The catch is the type. The map might not have the key, so the brackets give a String?, a string that can be null. The function must return a plain String, and Kotlin won't convert one into the other on its own. The lesson says the name is always present, so the not-null assertion !! fits: it tells the compiler "this isn't null" and gives you a String.
Step by step
- Read the value with square brackets using
nameas the key. - Add
!!to turn theString?into aString, then return it.
The solution in Kotlin
class Solution {
fun phoneBookLookup(phoneBook: MutableMap<String, String>, name: String): String {
return phoneBook[name]!!
}
}
Some details worth knowing:
- Brackets on a map call
get.phoneBook[name]is the same asphoneBook.get(name), and for a missing key it returnsnull. For a map with only"Ana"in it,pb["Zed"]printsnull. !!checks at run time. If the value isnull,!!throws aNullPointerException. Calling the function with"Zed"on that map throws it. That is acceptable here only because the lesson guarantees the key is present.- The map type.
MutableMap<String, String>is a map that can be changed. Reading works the same on a read-onlyMap.
Other ways to read a value
Kotlin has a few more ways to read a key, and they differ in what they do for a missing one:
map[key]returnsnull.map.getValue(key)returns a plainStringhere, and throws aNoSuchElementExceptionfor a missing key, with the messageKey Zed is missing in the map.for the key"Zed".map[key] ?: "unknown"supplies a replacement when the value isnull.
When a missing key is a bug, !! or getValue fails loudly, which is often what you want. When it is a normal case, give it a default instead. The next lessons on maps deal with that case.
Complexity
Time is O(1) on average: a hash map finds a key without scanning the other entries, so a phone book with a million names answers as fast as one with three. Space is O(1) extra, because the function stores nothing new.
Edge cases and common mistakes
- Leaving out
!!.return phoneBook[name]doesn't compile, because aString?can't be returned where aStringis declared. - Searching the values. The lookup goes from key to value. Equal numbers under different names don't matter.
- Different spelling. Keys are compared exactly, so
"ben"is not"Ben". The lesson only asks about names that are present.
Testing
{"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}with"Ben"gives555-0199.{"Max": "555-0100"}with"Max"gives555-0100.{"Tom": "555-0111", "Tim": "555-0111"}with"Tim"gives555-0111.
