How to remove elements from HashMap
To remove elements from HashMap we can use either of the 2 ways
1. Use the remove(Object key) method
2. Remove elements while iterating the HashMap
Lets see some examples for both these ways
1. Use the remove(Object key) to remove elements from HashMap
remove(Object key) method removes the mapping of the key/value from the HashMap and return the previous value that was removed.
Please note if the remove() method returns a null value, that doesn’t necessarily mean that no object/element was removed. This would be a case where a key with NULL value was stored in the HashMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.kscodes.sampleproject; import java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); hashMap.put("USD", "American Dollar"); hashMap.put("AUD", "Australian Dollar"); hashMap.put("EUR", "Euro"); System.out.println("The HashMap has following elements::"); System.out.println(hashMap); System.out.println("*********************************"); System.out.println("Now trying to remove element with key = 'AUD'"); // Trying to remove element with key = 'AUD' String oldValue = hashMap.remove("AUD"); System.out.println("The Old Value of the Key 'AUD' that was removed ::" + oldValue); System.out.println("The HashMap now has following elements::"); System.out.println(hashMap); } } |
Output
1 2 3 4 5 6 7 | The HashMap has following elements:: {EUR=Euro, USD=American Dollar, AUD=Australian Dollar} ********************************* Now trying to remove element with key = 'AUD' The Old Value of the Key 'AUD' that was removed ::Australian Dollar The HashMap now has following elements:: {EUR=Euro, USD=American Dollar} |
2. Use the Iterator to remove elements from HashMap
Iterator can be also used to remove all or a specific element from HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.kscodes.sampleproject; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); hashMap.put("USD", "American Dollar"); hashMap.put("AUD", "Australian Dollar"); hashMap.put("EUR", "Euro"); System.out.println("The HashMap has following elements::"); System.out.println(hashMap); System.out.println("*********************************"); System.out.println("Now trying to remove element with key = 'AUD'"); for (Iterator<Map.Entry<String, String>> it = hashMap.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, String> entry = it.next(); if (entry.getKey().equals("AUD")) { it.remove(); } } System.out.println("The HashMap now has following elements::"); System.out.println(hashMap); } } |
1 2 3 4 5 6 | The HashMap has following elements:: {EUR=Euro, USD=American Dollar, AUD=Australian Dollar} ********************************* Now trying to remove element with key = 'AUD' The HashMap now has following elements:: {EUR=Euro, USD=American Dollar} |