Java Hashtable - containsKey() Method
The java.util.Hashtable.containsKey() method returns true if the specified object is a key in this hashtable.
Syntax
public boolean containsKey(Object key)
Parameters
key |
Specify the key whose presence in this hashtable is to be tested. |
Return Value
Returns true the specified object is a key in this hashtable, else returns false.
Exception
NA.
Example:
In the example below, the java.util.Hashtable.containsKey() method is used to check the presence of specified key in the given hashtable.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating hashtable Hashtable<Integer, String> Htable = new Hashtable<Integer, String>(); //populating hashtable Htable.put(101, "John"); Htable.put(102, "Marry"); Htable.put(103, "Kim"); Htable.put(104, "Jo"); //check the presence of 103 key System.out.print("Does Htable contain 103 key? - "); System.out.print(Htable.containsKey(103)); //check the presence of 105 key System.out.print("\nDoes Htable contain 105 key? - "); System.out.print(Htable.containsKey(105)); } }
The output of the above code will be:
Does Htable contain 103 key? - true Does Htable contain 105 key? - false
❮ Java.util - Hashtable