![]() |
VOOZH | about |
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.
Initial HashMap: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=for}
After clear(): {}
public void clear()
Example 2: Here, we will use the clear() method to clear a HashMap of String keys and Integer values.
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.