Java MonthDay hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 20:59:01 作者:

Java MonthDay hashCode()方法及示例

MonthDay 类的 hashCode() 方法用于获取这个MonthDay的hashCode。如果对象没有变化,哈希码总是相同的。哈希码是在对象创建时由JVM生成的唯一代码。它可以用来对散列相关的算法进行一些操作,如hashable, hashmap等。一个对象也可以用它的唯一代码(哈希码)进行搜索。

语法

public int hashCode()

参数: 该方法不接受任何参数。

返回值: 该方法返回一个合适的哈希代码。

以下程序说明了hashCode()方法:

程序1 :

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

输出。

hashCode of YearMonth: 652

程序2

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

输出。

hashCode of YearMonth: 543

参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#hashCode()

相关推荐