- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 key value pair from HashMap?
Let’s say the following is our HashMap −
HashMap<String, String>map = new HashMap<String, String>();
Add key value pair to the HashMap −
map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); map.put("4", "D"); map.put("5", "E"); map.put("6", "F"); map.put("7", "G"); map.put("8", "H"); map.put("9", "I");
Now, use the remove() method to remove the key-value pair −
map.remove("5");
Example
import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; public class Demo { public static void main(String[] args) { HashMap<String, String>map = new HashMap<String, String>(); map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); map.put("4", "D"); map.put("5", "E"); map.put("6", "F"); map.put("7", "G"); map.put("8", "H"); map.put("9", "I"); System.out.println("HashMap..."); for (String res: map.keySet()) { System.out.println(res); } map.remove("5"); System.out.println("Updated HashMap..."); for (String res: map.keySet()) { System.out.println(res); } map.remove("8"); System.out.println("Updated HashMap..."); for (String res: map.keySet()) { System.out.println(res); } } }
Output
HashMap... 1 2 3 4 5 6 7 8 9 Updated HashMap... 1 2 3 4 6 7 8 9 Updated HashMap... 1 2 3 4 6 7 9
- Related Articles
- Java Program to remove a key from a HashMap
- Java Program to Get key from HashMap using the value
- Remove value from HashMap in Java
- Java Program to remove a key from a HashMap only if it is associated with a given value
- Java Program to Update value of HashMap using key
- Java Program to create a HashMap and add key-value pairs
- Java Program to replace key and value in HashMap with identical key and different values
- How to use null value as key in Java HashMap
- Remove all values from HashMap in Java
- C++ Remove an Entry Using Key from HashMap while Iterating Over It
- Get the value associated with a given key in Java HashMap
- Modify the value associated with a given key in Java HashMap
- Java Program to remove a key from a TreeMap only if it is associated with a given value
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- Swift Program to Find Maximum Key-Value Pair in the Dictionary

Advertisements