Java.lang Package Classes

Java Enum - hashCode() Method



The java.lang.Enum.hashCode() method returns a hash code for this enum constant.

Syntax

public final int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code for this enum constant.

Exception

NA.

Example:

In the example below, the java.lang.Enum.hashCode() method returns a hash code for the given enum constant.

import java.lang.*;

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

  public static void main(String[] args) {
    
    //printing the hascode of enum constant
    System.out.print("hashCode of weekday.MON is: ");
    System.out.println(weekday.MON.hashCode());
    System.out.print("hashCode of weekday.WED is: ");
    System.out.println(weekday.WED.hashCode());
    System.out.print("hashCode of weekday.FRI is: ");
    System.out.println(weekday.FRI.hashCode());    
  }
}

The output of the above code will be:

hashCode of weekday.MON is: 1995265320
hashCode of weekday.WED is: 746292446
hashCode of weekday.FRI is: 1072591677

❮ Java.lang - Enum