Sunday 13 April 2014

isEmpty : Check whether Map is Empty or not

boolean isEmpty()
Returns true if this map contains no key-value mappings.

import java.util.*;
class MapEmpty{
  public static void main(String args[]){
    Map<Integer, String> studentTable1 = new TreeMap<> ();
  Map<Integer, String> studentTable2 = new TreeMap<> ();
  
  /* Add Student details to studentTable1 */
  studentTable1.put(123, "Krishna");
  studentTable1.put(124, "Ram");
  studentTable1.put(125, "Anil");
  studentTable1.put(126, "Kiran");
  
  System.out.println("\nData in studentTable1 is");
  System.out.println(studentTable1);
  
  System.out.println("\nData in studentTable2 is");
  System.out.println(studentTable2);
  
  System.out.print("\nIs studentTable1 Empty ");
  System.out.println(studentTable1.isEmpty());
  
  System.out.print("\nIs studentTable2 Empty ");
  System.out.println(studentTable2.isEmpty());
  }
}

Output
Data in studentTable1 is
{123=Krishna, 124=Ram, 125=Anil, 126=Kiran}

Data in studentTable2 is
{}

Is studentTable1 Empty false

Is studentTable2 Empty true
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment