Problem overview
The method phoneBookLookup receives phoneBook, a HashMap from names to phone numbers, and a name. It returns the phone number stored for that name. Both keys and values are strings, and the name is always a key of the map.
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"}, looking up "Tim" returns "555-0111". Only the key is searched, never the value.
Approach: ask the map for the key
A HashMap stores key-value pairs and finds the value for a key directly, without scanning the entries one by one. That makes the whole lesson a single call: ask the map for the value stored under name.
Step by step
- Read the value with
getand return it.phoneBook.get(name)gives back the object stored under that key, here the phone number string.
The solution in Java
class Solution {
public String phoneBookLookup(HashMap<String, String> phoneBook, String name) {
return phoneBook.get(name);
}
}
Things to know about get:
- The return type matches the value type. The map is a
HashMap<String, String>, sogetreturns aString, which is what the method declares. - A missing key gives
null.getdoesn't throw for an unknown name. With the book above,phoneBook.get("Zed")returnsnull. This lesson promises the name is present, so no check is needed. - Keys must match exactly. Lookup uses
equalson the key, so"ben"does not find"Ben".
A few HashMap operations
A HashMap is built from the operations put, get, containsKey and size. With Ana and Ben stored, get("Ana") gives 555-0142, containsKey("Ben") is true, containsKey("Zed") is false and size() is 2. Calling put("Ana", "555-0000") again replaces Ana's number and leaves the size at 2, because a map holds one value per key.
That last point explains the lesson's statement that names are all different: a map can't hold two entries under one key. Different keys can share a value, so two people with the same number are fine.
Reading the method's signature
The parameter type, HashMap<String, String>, names two things inside the angle brackets: the type of the keys, then the type of the values. Both are String here, so get takes a String and returns a String. A map from names to counts would be HashMap<String, Integer>, as in the next lessons. The lookup itself is fast: get doesn't loop over the entries, so it takes about the same time on a book of ten names as on a book of a million. The key's type matters too: get looks for a key equal to the argument, so a name in the wrong spelling finds nothing.
Edge cases and common mistakes
- Single entry.
{"Max": "555-0100"}with"Max"returns"555-0100". Nothing special happens. - Shared numbers. Equal values don't confuse the lookup, because the key decides which entry is read.
- Using
phoneBook[name]. Square brackets are for arrays and don't work on aHashMap. Useget. - Printing instead of returning. The method has to return the string.
Testing
- The three-person book above with
"Ben"gives555-0199. {"Max": "555-0100"}with"Max"gives555-0100.{"Tom": "555-0111", "Tim": "555-0111"}with"Tim"gives555-0111.
