isEmpty() method of Map Interface in Java.

isEmpty() method of Map Interface in Java.


Posted in : Core Java Posted on : February 18, 2011 at 8:14 PM Comments : [ 0 ]

In this method you can check whether the map is empty or it contains key-value mappings. It returns 'true' when map is empty otherwise, returns false if key-value mapping contained by the map.

isEmpty() method of Map Interface in Java.

In this section we will discuss how can isEmpty() method be implemented in Map interface in java.

Syntax

boolean isEmpty()

This method check whether the underlying map has the ' key-value' mappings, or not.

In this method you can check whether the map is empty or it keeps 'key-value' mappings. It returns 'true' when map is empty otherwise, returns false if 'key-value' mapping existed into the map.

Parameter description

This method has no parameter but, it returns true if map has not the key-value mappings.

Example of isEmpty() method

In this example we will show you how does isEmpty() method work in Map interface. This example will help you to understand how you can check into map that is there any key-value mapping exists or not.

Example :

package devmanuals.com;
import java.util.Map;
import java.util.HashMap;
public class MapIsEmpty {
  public static void main(String args[]){
    Map mp = new HashMap();
    System.out.println("Is map empty ? "+mp.isEmpty());
    mp.put(1"A");
    mp.put(2"B");
    mp.put(3"C");
    mp.put(4"D");
    mp.put(5"E");
    boolean bol=mp.isEmpty();
    System.out.println("Is map empty ? "+bol);
    System.out.println("All the mappings are "+mp);
    System.out.println("Size of map = "+mp.size());
  }
}

Output :

Is map empty ? true

Is map empty ? false

All the mappings are {1=A, 2=B, 3=C, 4=D, 5=E}

Size of map = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics