Java Collection Tutorial - Java HashMap.isEmpty()


HashMap.isEmpty() has the following syntax.

public boolean isEmpty()


In the following code shows how to use HashMap.isEmpty() method.

//from w  ww . ja  va2s . c  om
import java.util.HashMap;

public class Main {
   public static void main(String args[]) {
      HashMap<Integer, String> newmap = new HashMap<Integer, String>();      
      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "from");
      newmap.put(3, "");
      // check if map is empty
      boolean val=newmap.isEmpty();
      // check the boolean value
      System.out.println("Is hash map empty: " + val);

The code above generates the following result.