Java HashMapremove() Method
Example
Remove entries from a map:
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
Theremove() 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:
public V remove(Objectkey)public boolean remove(Objectkey, Objectvalue)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 returnstrue if an entry was deleted andfalse otherwise. If no value is specified then it returns the value of the removed entry, ornull if an entry with the specified key does not exist. |
|---|
Related Pages
❮ HashMap Methods

