MonthDay hashCode() method in Java with Examples

Last Updated : 11 Apr, 2023

hashCode() method of the MonthDay class used to get hashCode for this MonthDay. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like a hashtable, hashmap etc. An object can also be searched with its unique code (hashcode). 

Syntax:

public int hashCode()

Parameters: This method did not accepts any parameter. 

Return value: This method returns a suitable hash code. 

Below programs illustrate the hashCode() method: 

Program 1: 

Java
// Java program to demonstrate
// MonthDay.hashCode() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a MonthDay object
        MonthDay month = MonthDay.parse("--10-12");

        // print hashcode
        System.out.println("hashCode"
                           + " of YearMonth: "
                           + month.hashCode());
    }
}
Output:
hashCode of YearMonth: 652

Program 2: 

Java
// Java program to demonstrate
// MonthDay.hashcode() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a MonthDay object
        MonthDay month = MonthDay.parse("--08-31");

        // print hashcode
        System.out.println("hashCode"
                           + " of YearMonth: "
                           + month.hashCode());
    }
}
Comment