Start learning free

LESSON 128 OF 671 · DICTIONARIES

Deleting a Key from a Java HashMap

Lesson Read the full explanation

TL ; DR:

A HashMap's remove method takes a key. It deletes that key and its value.


HashMap<String, Integer> toys = new HashMap<>();
toys.put("ball", 3);
toys.put("kite", 1);

toys.remove("kite");

System.out.println(toys.size());   // 1


Full lesson:

Sometimes an entry has to go. A shop stops selling an item. A player leaves the game.

Call remove on the map with the key. The key and its value go away together.

The size drops by 1. The key is gone, so toys.containsKey("kite") is now false.

remove also returns the value it deleted. For a missing key it returns null, and nothing changes.

Watch out: on an ArrayList, remove(2) takes out the item at index 2. On a HashMap, remove always takes a key.


Your task

A pet shop counts what it has for sale in animals. Each key is a kind of animal. Its value is how many the shop has.

The last rabbit just went home with a family. Delete the key "rabbit" from animals.

Then print animals.size() again. The code at the bottom checks if any rabbits are left.

Expected output:

3
2
No rabbits left
STEP 1 OF 2
    Ctrl + Enter to check

    JAVA · DICTIONARIES

    Deleting Dictionary Keys in Java, explained

    Removing an entry

    Entries sometimes have to go: a shop stops selling an item, a player leaves the game. HashMap's remove method takes a key and deletes that key together with its value.

    HashMap<String, Integer> animals = new HashMap<>();
    animals.put("cat", 4);
    animals.put("rabbit", 1);
    
    animals.remove("rabbit");
    
    System.out.println(animals.size());                  // 1
    System.out.println(animals.containsKey("rabbit"));   // false
    

    The size drops by 1, and the key is gone, so containsKey("rabbit") is now false.

    What remove returns

    remove returns the value it deleted. If the key is missing, it changes nothing and returns null; it does not throw.

    Integer gone = animals.remove("cat");
    System.out.println(gone);                     // 4
    System.out.println(animals.remove("cat"));    // null
    

    The return type is Integer rather than int because it may be null. Ignoring the return value, as the first example did, is perfectly fine.

    Using the returned value

    Because a missing key gives null, the return value tells you whether anything was removed. With one rabbit in the map, if (animals.remove("rabbit") != null) is true the first time. The second time the same condition is false, so a program can print nothing to remove in that case. Most of the time you do not need this and can ignore the result.

    The trap: remove on a list takes an index

    On an ArrayList, remove(2) removes the item at index 2. A HashMap has no positions, so its remove always takes a key. There is one place where the two look alike: a map with Integer keys. In a HashMap<Integer, String> called byId with the keys 2 (value "two") and 0 (value "zero"), byId.remove(2) removes the entry whose key is 2 and returns "two", leaving {0=zero}. It does not remove the item at position 2, because a map has no positions. Whenever you call remove, ask yourself whether the thing in front of you is a list or a map.

    Related methods

    • remove(key, value) with two arguments deletes the entry only if the key currently has that value. With apples at 5, remove("apples", 4) returns false and leaves the entry, while remove("apples", 5) returns true and removes it.
    • clear() removes every entry. Afterwards size() is 0 and isEmpty() is true.

    The exercise

    A pet shop keeps animals: cat 4, dog 2, rabbit 1. The program prints the size (3), and at the bottom checks whether any rabbits are left. The last rabbit has gone home with a family:

    1. Delete the key with animals.remove("rabbit"), below the first print.
    2. Print the size again with System.out.println(animals.size()). One key is gone, so it is 2.

    The final if then finds no "rabbit" key and takes its else branch.

    HashMap<String, Integer> animals = new HashMap<>();
    animals.put("cat", 4);
    animals.put("dog", 2);
    animals.put("rabbit", 1);
    System.out.println(animals.size());
    
    animals.remove("rabbit");
    System.out.println(animals.size());
    
    if (animals.containsKey("rabbit")) {
        System.out.println("We still have rabbits");
    } else {
        System.out.println("No rabbits left");
    }
    

    Output:

    3
    2
    No rabbits left
    

    Common mistakes

    • Setting the count to 0 instead of removing the key. After animals.put("rabbit", 0) the size is still 3 and containsKey("rabbit") is still true, so the program would say We still have rabbits. Only remove deletes the key.
    • Expecting an error for a missing key. Removing a key twice is harmless: the second call returns null and changes nothing.
    • Reading the key after removal. animals.get("rabbit") is null once the key is gone.

    Key takeaways

    • map.remove(key) deletes the key and its value, and the size goes down by 1.
    • It returns the old value, or null if the key was not there.
    • On a map, remove always takes a key, never an index.
    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free