This example shows how to clear HashMap in Java or how to delete all key value mappings from the HashMap object using the clear method of the HashMap class.
How to clear HashMap in Java?
There are a couple of ways using which you can delete all mappings from the HashMap.
1. Using the clear method
The clear
method of the HashMap class removes all key-value mappings from the HashMap object.
1 |
public void clear() |
The clear
method does not take any arguments and its return type is void. The HashMap object will be empty after this method call.
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 |
import java.util.HashMap; public class HashMapClearExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(2, "Three"); System.out.println("is empty? " + hMapNumbers.isEmpty() ); /* * To remove all mappings from the HashMap use * the clear method */ hMapNumbers.clear(); /* * The map object will be empty * after the clear method call */ System.out.println("is empty? " + hMapNumbers.isEmpty() ); } } |
Output
1 2 |
is empty? false is empty? true |
2. By assigning a new object
You can also assign a new empty HashMap object to the same reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.HashMap; public class HashMapClearExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(2, "Three"); System.out.println("is empty? " + hMapNumbers.isEmpty() ); //assign a new object to the same reference hMapNumbers = new HashMap<Integer, String>(); //this should return true System.out.println("is empty? " + hMapNumbers.isEmpty() ); } } |
Output
1 2 |
is empty? false is empty? true |
What is the preferred way to clear the HashMap?
Let’s first have a look at the source code of the clear
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Removes all of the mappings from this map. * The map will be empty after this call returns. */ public void clear() { Node<K,V>[] tab; modCount++; if ((tab = table) != null && size > 0) { size = 0; for (int i = 0; i < tab.length; ++i) tab[i] = null; } } |
The clear
method iterates the internal table and makes each entry null. All the key value references are set to null so that they be can garbage collected if they are not referenced anywhere else.
When you assign a new HashMap object to the same reference, similar to the clear
method, all the key value objects need to be garbage collected (if they are not referenced anywhere else) plus the original HashMap object. Additionally, a new map object needs to be created and assigned to the same reference.
The important difference is that when you use the clear
method, the same HashMap object can be reused. But when you assign a new object to the same reference, a new object needs to be created which is a costly operation in terms of performance.
Plus, as you can see from the code above, when you use the clear
method the HashMap capacity does not change. So if you are going to add a roughly similar number of mappings to the HashMap, the clear
method has an advantage as the table does not need to increase the capacity.
This tutorial is a part of the Java HashMap tutorial with examples.
Please let me know your views in the comments section below.