Java Period hashCode()方法及示例

Java Period hashCode()方法及示例

Java中Period类的hashCode()方法用于获取该周期生成的hashCode。

语法

public int hashCode()

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

返回值: 该方法返回为给定周期生成的hashCode。

以下程序说明了Java中的hashCode()方法。

程序1 :

// Java code to show the function hashCode()
// to get the hashCode for the given period
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to generate hashCode for the given period
    static void getNumberOfDays(int year, int months, int days)
    {
        Period period = Period.of(year, months, days);
        System.out.println(period.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int year = 12;
        int months = 3;
        int days = 31;
  
        getNumberOfDays(year, months, days);
    }
}

输出:

2032396

程序2 :

// Java code to show the function hashCode()
// to get the hashCode for the given period
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to generate hashCode for the given period
    static void getNumberOfDays(int year, int months, int days)
    {
        Period period = Period.of(year, months, days);
        System.out.println(period.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int year = -12;
        int months = 3;
        int days = 31;
  
        getNumberOfDays(year, months, days);
    }
}

输出:

2032372

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程