Check an Key Is Exists in Java HashMap Example

Check an Key Is Exists in Java HashMap Example


Posted in : Java Posted on : December 2, 2010 at 5:44 PM Comments : [ 0 ]

This Java Example shows how to know that an Key is exists in the specified HashMap object in java.

Check an Key Is Exists in Java HashMap Example

This Java Example shows how to know that an Key is exists in the specified HashMap object  in java. To get the Key from HashMap object we use boolean containsKey(key) method. This method returns true if list contains the specified key otherwise returns false.

 Example: ContainskeyInHashMap.java

package devmanuals.com;

import java.util.*;

public class ContainskeyInHashMap {
	public static void main(String args[]) {
		HashMap hm = new HashMap();
		hm.put(1, "Gyan");
		hm.put(2, "Ankit");
		hm.put(3, "Arun");
		hm.put(4, "Anand");
		hm.put(5, "Ram");
		boolean bool = hm.containsKey("2");
		System.out.println("Is 2 is exists in HashMap : " + bool);

	}
}

Output:

Is 2 is exists in HashMap : false

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics