VOOZH about

URL: https://www.geeksforgeeks.org/java/hashmap-clear-method-in-java/

⇱ HashMap clear() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HashMap clear() Method in Java

Last Updated : 6 Aug, 2025

The clear() method of the HashMap class in Java is used to remove all of the elements or mappings (key-value pairs) from a specified HashMap.

Example 2: Here, we will use the clear() method to clear a HashMap of Integer keys and String values.


Output
Initial HashMap: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=for}
After clear(): {}

Syntax of HashMap clear() Method

public void clear()

  • Parameters: This method does not accept any parameters.
  • Return Value: This method does not return any value.

Example 2: Here, we will use the clear() method to clear a HashMap of String keys and Integer values.


Output
Initial HashMap: {Geeks=20, for=15, You=30, Welcomes=25}
After clear(): {}

Note: The HashMap will remain empty after invoking the clear() method, but the HashMap object still exists and can be reused.

Comment