VOOZH about

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

⇱ Java HashMap isEmpty() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java HashMap isEmpty() Method

Last Updated : 11 Jul, 2025

The isEmpty() method in Java HashMap class is used to check whether the map contains any key-value mappings.

Example 1: Checking isEmpty() after adding entries to the map


Output
The Mappings are: {Java=1, Language=3, Programming=2}
Is the map empty? false

Syntax of isEmpty() Method

public boolean isEmpty()

Return Type:

  • It returns true, if the map contains no key-value mapping.
  • It returns false, if the map has one or more key-value mapping.

Example 2: Check isEmpty() Before and After Adding Entries


Output
HashMap contents: {}
Is the map empty? true
Updated HashMap contents: {1=Java, 2=Programming, 3=Language}
Is the map empty? false

Example 3: Check isEmpty() on an Empty HashMap


Output
The Mappings are: {}
Is the map empty? true
Comment