bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java HashMap remove() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java HashMap remove() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.HashMap;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The remove() method removes an entry with a specified key from the map.
Definition and Usage
Java HashMap remove() Method

❮ HashMap Methods

Example

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.

One of the following

public V remove(Object
key )
public boolean remove(Object
key , Object
value )

V refers to the data type of the values in the map.

Parameter Values

ParameterDescription
keyRequired. The key of the entry to be removed.
valueOptional. 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.

Previous

Java HashMap putIfAbsent() Method

Next

Java HashMap replace() Method