Home Tutorial Java Core HashMap Example of containsKey method of HashMap.

 
 

Example of containsKey method of HashMap.
Posted on: April 5, 2011 at 12:00 AM
Example of containsKey method of HashMap.

Example of containsKey method of HashMap.

The containsKey is a method of HashMap class. It always returns boolean value. It checks that the key is present or not in HashMap. 

If the given key is present in map, than is returns true othewise false.

Code

HashMapContainsKey.java

package net.roseindia.java;

import java.util.HashMap;

public class HashMapContainsKey {

public static void main(String[] arr) {

/* Create object of HashMap */

HashMap<Integer, String> obMap = new HashMap<Integer, String>();

/* Add value in HashMap */

obMap.put(new Integer(1), "Bharat");

obMap.put(new Integer(2), "Gyan");

obMap.put(new Integer(4), "Vrishti");

obMap.put(new Integer(3), "Sarika");

obMap.put(new Integer(5), "Rohit");

obMap.put(new Integer(6), "Parineeta");

/* Check key present in HashMap or not */

System.out.println("Returns :" + obMap.containsKey(1));

System.out.println("Returns :" + obMap.containsKey(8)); } }

Output :

Returns :true

Returns :false

Related Tags for Example of containsKey method of HashMap.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.