Start learning free

LESSON 129 OF 671 · DICTIONARIES - CHALLENGES

Phone Book Lookup in Java: HashMap get

Practice Read the full explanation

Given a phone book phoneBook, a HashMap from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · DICTIONARIES - CHALLENGES

    Phone Book Lookup in Java, explained

    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

    1. Read the value with get and 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>, so get returns a String, which is what the method declares.
    • A missing key gives null. get doesn't throw for an unknown name. With the book above, phoneBook.get("Zed") returns null. This lesson promises the name is present, so no check is needed.
    • Keys must match exactly. Lookup uses equals on 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 a HashMap. Use get.
    • Printing instead of returning. The method has to return the string.

    Testing

    • The three-person book above with "Ben" gives 555-0199.
    • {"Max": "555-0100"} with "Max" gives 555-0100.
    • {"Tom": "555-0111", "Tim": "555-0111"} with "Tim" gives 555-0111.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free