3:53 pm - Saturday March 25, 8789

Check if a particular key exists in HashMap example

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

Here is an example program for checking  the key  which exists in a hashmap


//*@author :CandidJava.com

//*@descriptions : This program explain the function and use of Key method to find that the value is present or not
import java.util.HashMap;

class CheckKeyOfHashMap  {

public static void main(String[] args) {

//creating HashMap Object

HashMap hMap = new HashMap();

hMap.put("1","One");

hMap.put("2","Two");

hMap.put("3","Three");

//to find the value present in the table using containsKey() method

boolean blnExists = hMap.containsKey("3");

System.out.println("3 exists in HashMap ? : " + blnExists);

}

}

Output:

D:\CandidJava\Util\HashMap>javac CheckKeyOfHashMap.java

Note: CheckKeyOfHashMap.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashMap>java CheckKeyOfHashMap

3 exists in HashMap ? : true

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply