Java中的即时hashCode()方法

可以使用hashCode()Java中Instant类中的方法来获取特定Instant对象的哈希码。此方法不需要任何参数,它返回Instant对象的哈希码。

演示此程序如下

示例

import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
   public static void main(String[] args) {
      Instant i = Instant.now();
      System.out.println("The current instant is: " + i);
      int hashCode = i.hashCode();
      System.out.println("The Hash Code value for the instant is: " + hashCode);
   }
}

输出结果

The current instant is: 2019-02-13T12:05:21.488Z
The Hash Code value for the instant is: 668255745

现在让我们了解上面的程序。

首先显示当前时刻。然后,使用该hashCode()方法获取该Instant的哈希码,并显示该哈希码。演示此代码段如下所示:

Instant i = Instant.now();
System.out.println("The current instant is: " + i);
int hashCode = i.hashCode();
System.out.println("The Hash Code value for the instant is: " + hashCode);