Java Utility Library

Java Hashtable - containsValue() Method



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

Syntax

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