Java Utility Library

Java IdentityHashMap - hashCode() Method



The java.util.IdentityHashMap.hashCode() method returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code value for this map.

Exception

NA.

Example:

In the example below, the java.util.IdentityHashMap.hashCode() method returns the hash code value for the given identity hash map.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a identity hash map
    IdentityHashMap<Integer, String> MyMap = new IdentityHashMap<Integer, String>();

    //populating the map
    MyMap.put(101, "John");
    MyMap.put(102, "Marry");
    MyMap.put(103, "Kim");
    MyMap.put(104, "Jo");
    MyMap.put(105, "Sam");

    //printing the map
    System.out.println("MyMap contains: " + MyMap);   

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

The output of the above code will be:

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

❮ Java.util - IdentityHashMap