Java year hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 21:17:57 作者:

Java year hashCode()方法及示例

Java中Year类的hashCode()方法用于为当前Year对象获取合适的哈希代码。

语法:

public int hashCode()

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

返回值 :它为使用它的年份对象返回一个合适的积分哈希代码。它不会返回NULL值。

下面的程序说明了Java中年份的hashCode()方法:
程序1 :

// Program to illustrate the hashCode() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Creates a Year object        Year firstYear = Year.of(1997);          // Print the hash code for        // the current year object        System.out.println(firstYear.hashCode());    }}

输出:

1997

程序2 :

// Program to illustrate the hashCode() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Creates a Year object        Year firstYear = Year.of(2018);          // Print the hash code for        // the current year object        System.out.println(firstYear.hashCode());    }}

输出:

2018

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#hashCode-

相关推荐