Java Utility Library

Java Hashtable - contains() Method



The java.util.Hashtable.contains() method returns true if this hashtable maps one or more keys to this value.

Syntax

public boolean contains(Object value)

Parameters

value Specify the value whose presence in this hashtable is to be tested

Return Value

Returns true if this hashtable maps one or more keys to the specified value.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.contains() method is used to check whether this hashtable contains any key which is mapped to the specified value or not.

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 for value - "Kim"
    System.out.print("Does Htable contain key(s) mapped to 'Kim'? - ");  
    System.out.print(Htable.contains("Kim"));  
    //check for value - "Sam"
    System.out.print("\nDoes Htable contain key(s) mapped to 'Sam'? - ");  
    System.out.print(Htable.contains("Sam"));  
  }
}

The output of the above code will be:

Does Htable contain key(s) mapped to 'Kim'? - true
Does Htable contain key(s) mapped to 'Sam'? - false

❮ Java.util - Hashtable