Home »
Java »
Java Reference »
Java Duration Class
Java Duration Class | hashCode() Method with Example
Duration Class hashCode() method: Here, we are going to learn about the hashCode() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020
Duration Class hashCode() method
- hashCode() method is available in java.time package.
- hashCode() method is used to get the hash code value for this Duration.
- hashCode() method is a non-static method, it is accessible with class object only and if we try to access the method with class name then we will get an error.
- hashCode() method does not throw an exception at the time of returning hash code.
Syntax:
public int hashCode();
Parameter(s):
Return value:
The return type of this method is int, it retrieves this Duration hash code value.
Example:
// Java program to demonstrate the example
// of int hashCode() method of Duration
import java.time.*;
public class HashCodeOfDuration {
public static void main(String args[]) {
// Instantiates two Duration objects
Duration du1 = Duration.ofHours(1);
Duration du2 = Duration.parse("P1DT24H60M60S");
// Display du1 and du2
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
// returns the hash code
// value for this Duration du1
int hash_code = du1.hashCode();
// Display hash_code
System.out.println("du1.hashCode(): " + hash_code);
// returns the hash code
// value for this Duration du2
hash_code = du2.hashCode();
// Display hash_code
System.out.println("du2.hashCode(): " + hash_code);
}
}
Output
du1: PT1H
du2: PT49H1M
du1.hashCode(): 3600
du2.hashCode(): 176460