MonthDay hashCode() method in Java with Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

hashCode of YearMonth: 652

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// MonthDay.from() 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());
    }
}

chevron_right


Output:

hashCode of YearMonth: 543

References: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#hashCode()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.