In this article, we will discuss how to remove a particular entry from invoking HashMap
1. HashMap :
- remove(key) –> to delete a particular entry by specifying key as argument
- Note: above method inherited from Map interface
2. Removing an entry from HashMap
RemoveEntryOfHashMap.java
package in.bench.resources.java.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class RemoveEntryOfHashMap {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
HashMap<String, String> hashMap = new HashMap<String, String>();
// adding key-value pairs to HashMap object
hashMap.put("Google", "Sundar Pichai");
hashMap.put("Facebook", "Mark Zuckerberg");
hashMap.put("LinkedIn", "Reid Hoffman");
hashMap.put("Apple", "Steve Jobs");
hashMap.put("Microsoft", "Bill Gates");
System.out.println("Size of a HashMap is : "
+ hashMap.size());
System.out.println("\n\nBefore any operation :"
+ " Set of all Entries: \n");
// getting keySet() into Set
Set<String> set1 = hashMap.keySet();
// for-each loop
for(String key : set1) {
System.out.println("Key : " + key + "\t\t"
+ "Value : " + hashMap.get(key));
}
// deleting particular entry
hashMap.remove("Google");
System.out.println("\n\nHashMap size after removing : "
+ hashMap.size());
System.out.println("\n\nAfter remove operation :"
+ " Set of all Entries: \n");
// getting keySet() into Set
Set<String> set2 = hashMap.keySet();
// Collection Iterator
Iterator<String> itr = set2.iterator();
while(itr.hasNext()) {
String key = itr.next();
System.out.println("Key : " + key + "\t\t"
+ "Value : " + hashMap.get(key));
}
}
}
Output:
Size of a HashMap is : 5
Before any operation : Set of all Entries:
Key : LinkedIn Value : Reid Hoffman
Key : Microsoft Value : Bill Gates
Key : Facebook Value : Mark Zuckerberg
Key : Apple Value : Steve Jobs
Key : Google Value : Sundar Pichai
HashMap size after removing : 4
After remove operation : Set of all Entries:
Key : LinkedIn Value : Reid Hoffman
Key : Microsoft Value : Bill Gates
Key : Facebook Value : Mark Zuckerberg
Key : Apple Value : Steve Jobs
Related Articles:
- Java – How to get all keys of a HashMap ?
- Java – How to get all values of a HashMap ?
- Java – How to get all Entries or Key-Value pairs of HashMap ?
- Java – How to get size or length of HashMap ?
- Java – How to check whether a particular key is present in HashMap ?
- Java – How to check whether a particular value is present in HashMap ?
- Java – How to check whether HashMap is empty or not ?.
- Java – Adding one HashMap to another HashMap using putAll method
- Java – How to delete an entry of HashMap ?
- Java – How to delete all entries of HashMap ?
- Java 8 – How to remove an entry from HashMap by comparing values
- Java 8 – How to remove an entry from HashMap by comparing keys
- Java – Various ways to iterate through HashMap
- Java – To reverse order the LinkedHashMap contents
- Java – How ConcurrentModificationException can be handled ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!