The syntax of the isEmpty() method is:
hashmap.isEmpty()
Here, hashmap is an object of the HashMap class.
isEmpty() Parameters
The isEmpty() method does not take any parameters.
isEmpty() Return Value
- returns
trueif the hashmap does not contain any key/value mappings - returns
falseif the hashmap contains key/value mappings
Example: Check if HashMap is Empty
Output
Newly Created HashMap: {}
Is the HashMap empty? true
Updated HashMap: {Java=14, Python=1}
Is the HashMap empty? false
In the above example, we have created a hashmap named languages. Here, we have used the isEmpty() method to check whether the hashmap contains any elements or not.
Initially, the newly created hashmap does not contain any element. Hence, isEmpty() returns true. However, after we add some elements (Python, Java), the method returns false.