Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to remove a key from a HashMap
In this article, we will learn how to remove a specific key from a HashMap in Java. By doing this, we can dynamically update the contents of the map, which is helpful in many data manipulation scenarios.
We will demonstrate two different approaches: one that uses the straightforward remove() method to delete a specific key, and another that leverages an iterator to find and remove the key based on a condition while iterating. Each approach allows us to efficiently manage data in HashMap based on different requirements.
Problem Statement
Given a HashMap with key-value pairs, write Java programs to remove a specific key ?
Input
Elements in HashMap...
[Frames=800, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000]
Output
Elements in HashMap...
[Frames=800, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000]
Updated HashMap after removing a key...
{Frames=800, Belt=600, Bag=1100, Sunglasses=2000}
Different approaches
Following are the different approaches to remove a key from a HashMap ?
Using the remove() method
Following are the steps to remove a key from a HashMap using the remove() method ?
- First we will import all the classes from the java.util package.
- We initialize the HashMap and add several key-value pairs, where each item represents an object (like "Bag" or "Wallet") with an associated price.
- Before removing any items, we print the initial contents of the HashMap to show all key-value pairs.
- Using remove() method of HashMap class we delete the entry with the key "Wallet" from the HashMap.
- After removing the key, we print the HashMap again to show the updated content without the "Wallet" entry.
Example
The following is an example of removing a key from a HashMap ?
import java.util.*;
public class Demo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("Bag", new Integer(1100));
hm.put("Sunglasses", new Integer(2000));
hm.put("Frames", new Integer(800));
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
System.out.println("Elements in HashMap...");
System.out.println(hm);
hm.remove("Wallet");
System.out.println("\nUpdated HashMap after removing a key...");
System.out.println(hm);
}
}
Output
Elements in HashMap...
[Frames=800, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000]
Updated HashMap after removing a key...
{Frames=800, Belt=600, Bag=1100, Sunglasses=2000}
Using an iterator
Following are the steps of removing a key from a HashMap using an iterator ?
- First we will import the HashMap, Iterator, and Map class from java.util package.
- We will initialize the HashMap and add key-value pairs.
- We will use an iterator to loop through the HashMap.
- To remove a key while iterating if a certain conditionis met for this we will use the if statement.
- Print the HashMap before and after the removal.
Example
The following is an example of removing a key from a HashMap ?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Demo {
public static void main(String[] args) {
// Creating a HashMap
HashMap<String, Integer> map = new HashMap<>();
// Adding elements to HashMap
map.put("Apple", 100);
map.put("Banana", 150);
map.put("Grapes", 200);
System.out.println("Original Map: " + map);
// Using an iterator to remove a key
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
if (entry.getKey().equals("Banana")) {
iterator.remove(); // Removes "Banana" from the map
}
}
System.out.println("Updated Map after iterator removal: " + map);
}
}
Output
Original Map: {Apple=100, Grapes=200, Banana=150}
Updated Map after iterator removal: {Apple=100, Grapes=200}