📜  Java中的句点 hashCode() 方法及示例

📅  最后修改于: 2022-05-13 01:55:37.100000             🧑  作者: Mango

Java中的句点 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

方案二

// 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: Java/time/Period.html#hashCode–