Java Instant hashCode()方法及示例
Instant类的 hashCode() 方法是用来获取这个Instant的hashCode。如果对象没有变化,哈希码总是相同的。哈希码是在对象创建时由JVM生成的唯一代码。它可以用来对散列相关算法进行一些操作,如hashable,hashmap等。一个对象也可以用它的唯一代码(哈希码)进行搜索。
语法
public int hashCode()
返回: 该方法返回这个Instant的hashCode。
下面的程序说明了Instant.hashCode()方法。
程序1 :
// Java program to demonstrate
// Instant.hashCode() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-12-30T19:34:50.63Z");
// get hashCode
// using hashCode()
int value = instant.hashCode();
// print result
System.out.println("hashCode value: "
+ value);
}
}
输出:
hashCode value: -683539878
程序2
// Java program to demonstrate
// Instant.hashCode() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant = Instant.now();
// current Instant
System.out.println("Current Instant: "
+ instant);
// get hashCode
// using hashCode()
int value = instant.hashCode();
// print result
System.out.println("hashCode value: "
+ value);
}
}
输出:
Current Instant: 2018-11-27T04:45:52.428Z
hashCode value: 1896457472
参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#hashCode()