OptionalInt hashCode() method in Java with examples
Last Updated :
01 May, 2019
The hashCode() method help us to get the hash code of the value, if Int value is present, otherwise 0 (zero) if no Int value is present in OptionalInt object.
Syntax:
public int hashCode()
Parameters: This method accepts does not accepts any parameter.
Return value: This method returns hash code value of the present value or 0 if no value is present.
Below programs illustrate hashCode() method:
Program 1:
import java.util.OptionalInt;
public class GFG {
public static void main(String[] args)
{
OptionalInt opInt
= OptionalInt.of( 253 );
System.out.println( "OptionalInt: "
+ opInt.toString());
System.out.println( "HashCode value: "
+ opInt.hashCode());
}
}
|
Output:
OptionalInt: OptionalInt[253]
HashCode value: 253
Program 2:
import java.util.OptionalInt;
public class GFG {
public static void main(String[] args)
{
OptionalInt opInt
= OptionalInt.empty();
System.out.println( "OptionalInt: "
+ opInt.toString());
System.out.println( "HashCode value: "
+ opInt.hashCode());
}
}
|
Output:
OptionalInt: OptionalInt.empty
HashCode value: 0
References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#hashCode()
Share your thoughts in the comments
Please Login to comment...