Java HashMap isEmpty() method example

HQ ¬Ľ Java Tutorial ¬Ľ java.util ¬Ľ Hashmap ¬Ľ isEmpty() method example
Java HashMap isEmpty() method example 2018-02-16T07:27:22+00:00

java.util.HashMap isEmpty()

Description :

On this document we will be showing a java example on how to use the isEmpty() method of HashMap Class. Basically this method just returns true if the HashMap object contains zero key-value combinations. This method might be useful in situation wherein we have to test if the map is empty or not and do business logic depending on the result. Important notes for size method:

  • specified by isEmpty¬†in interface Map<K,V>
  • overrides isEmpty¬†in class AbstractMap<K,V>

Method Syntax :

public boolean isEmpty()

Parameter Input :

[table] DataType|Parameter|Description

Method Returns :

The size method returns a boolean value depending on the result:

  • true if the map contains no key-value pair
  • false if the map object contains at least one key-value pairs

Compatibility Version :

Requires Java 1.2 and up

Exception :


Java Code Example :

This java example source code demonstrates the use of isEmpty() method of HashMap class. From the example below, basically we just assign values to our HashMap at the start of the code. After which we then check if it is not empty. If the map is found to be not empty, we print out the contents. This is a good example in how to loop through the contents of HashMap. If you want to learn more on the usage of each of the methods that we have used here, please go through with each of the examples and documentation that we have provided located at the left navigation pane of this page.

Sample Output :

Running the above java example of the usage of HashMap isEmpty() method will give the following output:

HashHamp isEmpty() method example

HashHamp isEmpty() method example Output

Similar Method :

  • N/A

Suggested Reading List :

References :

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.