9:59 am - Sunday April 23, 2017

Check whether the HashMap is empty boolean isEmpty( )

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK  

                             This method is used to check whether the map contains key-value mappings or not. If there is no mappings (i.e empty), it returns true or else it returns false. If the hashMap size is zero the isEmpty function will return true.

Example Program

import java.util.HashMap;

public class Hash2 {

	public static void main(String[] args)

	{

		HashMap<String, Integer> h1 = new HashMap<String, Integer>();
		System.out.println("collections");

		System.out.println("List");

		h1.put("Linked list", 0);

		h1.put("Array List", 1);

		h1.put("Vector", 2);

		h1.put("Stack", 3);

		System.out.println(h1);

		if (h1.isEmpty())

		{

			System.out.println("Boolean value is:" + h1.isEmpty());

		}

		else

		{

			System.out.println("Boolean value is:" + h1.isEmpty());

		}

		h1.clear();

		System.out.println("After clearing:" + h1);

		if (h1.isEmpty())

		{

			System.out.println("Boolean value is:" + h1.isEmpty());

		}

		else

		{

			System.out.println("Boolean value is:" + h1.isEmpty());

		}

	}
}

Output

   Collections

   List

   {Stack=3, Array List=1, Linked list=0, Vector=2}

   Boolean value is:false

   After clearing:{}

   Boolean value is:true

BACK

Filed in: Java Examples

Comments are closed.