Skip to content
Related Articles

Related Articles

Improve Article

Instant hashCode() method in Java with Examples

  • Last Updated : 27 Nov, 2018

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);
    }
}
Output:
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);
    }
}
Output:
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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :