Java Utility Library

Java Hashtable - hashCode() Method



The java.util.Hashtable.hashCode() method returns the hash code value for this Map as per the definition in the Map interface.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code value for this hashtable.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.hashCode() method returns the hash code value for the given hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a 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");
    Htable.put(105, "Sam");

    //printing hashtable
    System.out.println("Htable contains: " + Htable);   

    //printing hashcode of the hashtable
    System.out.print("Hash Code of Htable is: ");
    System.out.print(Htable.hashCode());
  }
}

The output of the above code will be:

Htable contains: {105=Sam, 104=Jo, 103=Kim, 102=Marry, 101=John}
Hash Code of Htable is: 76589404

❮ Java.util - Hashtable