Instant hashCode() method in Java

Java 8Object Oriented ProgrammingProgramming

The hash code for a particular Instant object can be obtained using the hashCode() method in the Instant class in Java. This method requires no parameters and it returns the hash code for the Instant object.

A program that demonstrates this is given as follows

Example

 Live Demo

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);
   }
}

Output

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

Now let us understand the above program.

First the current instant is displayed. Then the hash code for that Instant is obtained using the hashCode() method and that is also displayed. A code snippet that demonstrates this is as follows:

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);
raja
Updated on 30-Jul-2019 22:30:25

Advertisements