Example
Remove entries from a map:
java
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
capitalCities.remove("USA");
capitalCities.remove("Germany", "Berlin");
capitalCities.remove("England", "Cambridge");
System.out.println(capitalCities);
}
}
Definition and Usage
The remove() method removes an entry with a specified key from the map. If a value is provided then the entry will only be removed if its value matches the specified value.
Syntax
One of the following:
java
public V remove(Object key)
java
public boolean remove(Object key, Object value)
V refers to the data type of the values in the map.
Parameter Values
| Parameter | Description |
|---|---|
| key | Required. The key of the entry to be removed. |
| value | Optional. The value of the entry to be removed. |
Technical Details
| Returns: | When a value is specified, it returns true if an entry was deleted and false otherwise. If no value is specified then it returns the value of the removed entry, or null if an entry with the specified key does not exist. |
|---|
Related Pages
Java HashMap Tutorial