How to Remove All Items From a HashMap in Java


In this example we will show the method to remove all items from a HashMap in Java.

Source Code

package com.beginner.examples;

import java.util.HashMap;

public class ClearHashMap {
  public static void main(String[] args) {
    HashMap maps = new HashMap();
    maps.put("A", "a");
    maps.put("B", "b");
    
    maps.clear(); // remove all items
    System.out.println(maps); 
  }
}

Output:

{}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments