java.util.Hashtable.containsKey() Method


The containsKey(Object key) method is used to test if the specified object is a key in this hashtable.


Following is the declaration for java.util.Hashtable.containsKey() method.

public boolean containsKey(Object key)


key − This is the a key to be searched.

Return Value

The method call returns true if and only if the specified object is a key in this hashtable.


NullPointerException − This is thrown if the key is null.


The following example shows the usage of java.util.Hashtable.containsKey()

package com.tutorialspoint;

import java.util.*;

public class HashTableDemo {
   public static void main(String args[]) {

      // create hash table 
      Hashtable htable = new Hashtable();

      // put values into the table
      htable.put(1, "A");
      htable.put(2, "B");
      htable.put(3, "C");
      htable.put(4, "D");

      // check if table contains key "3"
      boolean isavailable = htable.containsKey(3);

      // display search result
      System.out.println("Hash table contains key '3': "+isavailable);      

Let us compile and run the above program, this will produce the following result.

Hash table contains key '3': true
Kickstart Your Career

Get certified by completing the course

Get Started