Remove all elements from HashMap in Java

In this tutorial, we will learn about how to Remove all elements from HashMap in Java.

HashMap in Java is a hash table based implementation of Map Interface. It is used to store key-value pairs. HashMap is a widely used collection classes in java.  To access a value in HashMap, you need to have a key. If we want to delete/remove the elements from HashMap, this class provides clear() method to remove all elements from HashMap. The syntax of clear() method is:

Syntax of clear() method:

this method removes all the elements from this HashMap. The HashMap will be empty after this call returns.

Example:

Output:

In the above program, we have used clear() method to remove all the elements from HashMap.

Related posts

Leave a Comment