Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

remove()

Example

Eliminate 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

The remove() method deletes an entry with a given key from the map. If a value is supplied, the entry will be removed only if its value matches the specified value.

Syntax

Choose one of the following:

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

V denotes the data type of the values stored in the map.

Parameter Values

Parameter

Description

key

Necessary: The key of the entry to be eliminated.

value

Optional: The value associated with the entry to be removed.

Technical Details

Returns:

If a value is provided, it returns true if an entry was successfully deleted, and false otherwise. If no value is specified, it returns the value of the removed entry, or null if there’s no entry with the specified key.