java.util.HashMap.isEmpty() Method


Description

The isEmpty() method is used to check if this map contains no key-value mappings.

Declaration

Following is the declaration for java.util.HashMap.isEmpty() method.

public boolean isEmpty()

Parameters

NA

Return Value

The method call returns 'true' if this map contains no key-value mappings.

Exception

NA

Example

The following example shows the usage of java.util.HashMap.isEmpty()

package com.tutorialspoint;

import java.util.*;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap newmap = new HashMap();

      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best");

      // check if map is empty
      boolean val = newmap.isEmpty();

      // check the boolean value
      System.out.println("Is hash map empty: " + val);
   }    
}

Let us compile and run the above program, this will produce the following result.

Is hash map empty: false
java_util_hashmap.htm
Advertisements