Java Utility Library

Java EnumMap - hashCode() Method



The java.util.EnumMap.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.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns the hash code value for this map.

Exception

NA.

Example:

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

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    MON, TUE, WED, THU, FRI
  }

  public static void main(String[] args) {
    //creating EnumMaps
    EnumMap<weekday,Integer> MyMap = 
        new EnumMap<weekday,Integer>(weekday.class);

    //associate values in MyMap
    MyMap.put(weekday.MON, 1);
    MyMap.put(weekday.TUE, 2);
    MyMap.put(weekday.WED, 3);
    MyMap.put(weekday.THU, 4);
    MyMap.put(weekday.FRI, 5);

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

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

The output of the above code will be:

MyMap contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5}
Hash Code of MyMap is: -1763058631

❮ Java.util - EnumMap