OptionalInt hashCode() method in Java with examples
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:
// Java program to demonstrate // OptionalInt.hashCode() method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // create a OptionalInt instance OptionalInt opInt = OptionalInt.of(253); System.out.println("OptionalInt: " + opInt.toString()); // get hashCode value using hashCode() System.out.println("HashCode value: " + opInt.hashCode()); } } |
OptionalInt: OptionalInt[253] HashCode value: 253
Program 2:
// Java program to demonstrate // OptionalInt.hashCode() method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // create a OptionalInt instance OptionalInt opInt = OptionalInt.empty(); System.out.println("OptionalInt: " + opInt.toString()); // get hashCode value using hashCode() System.out.println("HashCode value: " + opInt.hashCode()); } } |
OptionalInt: OptionalInt.empty HashCode value: 0
References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#hashCode()
Recommended Posts:
- OptionalInt of(int) method in Java with examples
- OptionalInt getAsInt() method in Java with examples
- OptionalInt orElse(int) method in Java with examples
- OptionalInt orElseThrow() method in Java with examples
- OptionalInt empty() method in Java with examples
- OptionalInt orElseGet() method in Java with examples
- OptionalInt ifPresentOrElse() method in Java with examples
- OptionalInt isPresent() method in Java with examples
- OptionalInt stream() method in Java with examples
- OptionalInt equals() method in Java with examples
- OptionalInt toString() method in Java with examples
- OptionalInt orElseThrow(Supplier) method in Java with examples
- OptionalInt ifPresent(IntConsumer) method in Java with examples
- Set hashCode() method in Java with Examples
- Map hashCode() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


