English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java other topics

Java HashMap remove() usage and examples

Java HashMap Methods

The Java HashMap remove() method removes the mapping from the hash map associated with the specified key.

The syntax of remove() method is:

hashmap.remove(Object key, Object value);

remove() parameters

  • key - Deletes the mapping of the key

  • value(Optional)-Removes the mapping only if the specified key maps to the specified value

The return value of remove()

The remove() method removes the mapping and returns:

  • The previous value associated with the specified key

  •  Returns true if the mapping is deleted

NoteReturns null if the specified key maps to a null value or the key does not exist in the hash map.

Example1The remove() method of HashMap with key parameter

import java.util.HashMap;
class Main {
  public static void main(String[] args){
    //Create HashMap
    HashMap<Integer, String> languages = new HashMap<>();
    //Add mapping to HashMap
    languages.put(1, "Python");
    languages.put(2, "C");
    languages.put(3, "Java");
    System.out.println("Languages: ") + languages);
    //remove key2mapping
    languages.remove(2);  // return C
    System.out.println("Updated Languages: ") + languages);
  }
}

Output result

Languages: {1=Python, 2=C, 3=Java}
Updated Languages: {1=Python, 3=Java}

In the above example, we created a hash map named languages. Here, the remove() method does not have an optional value parameter. Therefore, the mapping with key2mapping has been removed from the hash map.

Example2: HashMap with key and value remove()

import java.util.HashMap;
class Main {
  public static void main(String[] args) {
    //Create a HashMap
    HashMap<String, String> countries = new HashMap<>();
    //Insert item into HashMap
    countries.put("Washington", "America");
    countries.put("Ottawa", "Canada");
    countries.put("Kathmandu", "Nepal");
    System.out.println("Countries: ") + countries);
    // Remove mapping {Ottawa=Canada}
    countries.remove("Ottawa", "Canada");  // return true
    // Remove mapping {Washington=USA}
    countries.remove("Washington", "USA");  // return false
    System.out.println("remove() after Countries: ") + countries);
  }
}

Output result

Countries: {Kathmandu=Nepal, Ottawa=Canada, Washington=America}
The 'remove()' method is followed by 'Countries: {Kathmandu=Nepal, Washington=America}'

In the above example, we created a hash map named 'countries'. Note this line,

countries.remove("Ottawa", "Canada");

Here, the remove() method includes an optional value parameter (Canada). Therefore, the mapping from the key 'Ottawa' to the value 'Canada' has been removed from the hash map.

But please note that

countries.remove("Washington", "USA");

In this case, the hash map does not contain a mapping with the key 'Washington' and value 'USA'. Therefore, there is no mapping 'Washington' = 'America' removed from the hash map.

NoteWe can useJava HashMap clear()Methods to remove all mappings from the hash map.

Java HashMap Methods