The hashCode() method of Instant Class is used to get hashCode for this Instant. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like hashtable, hashmap etc. An object can also be searched with its unique code (hashcode).
Syntax:
public int hashCode()
Returns: This method returns the hashCode for this Instant.
Below programs illustrate the Instant.hashCode() method:
Program 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
Program 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
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#hashCode()