java.util.Hashtable.containsKey() Method



Description

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

Declaration

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

public boolean containsKey(Object key)

Parameters

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.

Exception

NullPointerException − This is thrown if the key is null.

Example

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
java_util_hashtable.htm
Advertisements