The HashMap.remove()
method in Java is used to remove a mapping from a HashMap
for a specified key.
Table of Contents
- Introduction
remove
Method Syntax- Examples
- Removing a Key-Value Pair by Key
- Real-World Use Case: Removing Employee Records
- Conclusion
Introduction
The HashMap.remove()
method is a member of the HashMap
class in Java. It allows you to remove a key-value pair from the HashMap
by specifying the key. This can be useful when you need to delete specific entries from the map.
remove() Method Syntax
The syntax for the remove
method is as follows:
public V remove(Object key)
- The method takes a single parameter
key
of typeObject
, which represents the key whose mapping is to be removed from theHashMap
. - The method returns the value associated with the specified key, or
null
if the map contains no mapping for the key.
There is also an overloaded version of the remove
method:
public boolean remove(Object key, Object value)
- The method takes two parameters:
key
of typeObject
, which represents the key whose mapping is to be removed.value
of typeObject
, which represents the value expected to be associated with the specified key.
- The method returns a boolean value:
true
if the mapping was removed.false
if no mapping was removed.
Examples
Removing a Key-Value Pair by Key
The remove
method can be used to remove a key-value pair from a HashMap
by specifying the key.
Example
import java.util.HashMap;
import java.util.Map;
public class RemoveExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
countries.put("Canada", "CA");
// Removing an entry from the HashMap
String removedValue = countries.remove("United States");
// Printing the removed value and the updated HashMap
System.out.println("Removed value: " + removedValue);
System.out.println("Updated HashMap: " + countries);
}
}
Output:
Removed value: US
Updated HashMap: {India=IN, Canada=CA}
Real-World Use Case: Removing Employee Records
In a real-world scenario, you might use the remove
method to delete an employee record from an employee database.
Example
import java.util.HashMap;
import java.util.Map;
public class RemoveEmployeeRecord {
public static void main(String[] args) {
// Creating a Map with String keys (employee IDs) and String values (employee names)
Map<String, String> employeeDatabase = new HashMap<>();
// Adding entries to the HashMap
employeeDatabase.put("E001", "Ravi Kumar");
employeeDatabase.put("E002", "Priya Sharma");
employeeDatabase.put("E003", "Vijay Singh");
// Removing an employee record from the database
String removedEmployee = employeeDatabase.remove("E002");
// Printing the removed employee and the updated employee database
System.out.println("Removed Employee: " + removedEmployee);
System.out.println("Updated Employee Database: " + employeeDatabase);
}
}
Output:
Removed Employee: Priya Sharma
Updated Employee Database: {E001=Ravi Kumar, E003=Vijay Singh}
Removing a Key-Value Pair by Key and Value
The overloaded remove
method can be used to remove a key-value pair only if it is currently mapped to a specified value.
Example
import java.util.HashMap;
import java.util.Map;
public class RemoveByKeyValueExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
countries.put("Canada", "CA");
// Removing an entry by key and value
boolean isRemoved = countries.remove("United States", "US");
// Printing the result of the removal and the updated HashMap
System.out.println("Entry removed: " + isRemoved);
System.out.println("Updated HashMap: " + countries);
}
}
Output:
Entry removed: true
Updated HashMap: {India=IN, Canada=CA}
Conclusion
The HashMap.remove()
method in Java provides a way to remove key-value pairs from a HashMap
by specifying the key, and optionally the value. By understanding how to use this method, you can efficiently manage and modify the contents of your HashMap
. This method is useful in various scenarios, such as deleting records, managing collections of data, and ensuring data integrity.