Thursday, January 29, 2026
HomeLanguagesJavaOptionalLong hashCode() method in Java with examples

OptionalLong hashCode() method in Java with examples

The hashCode() method help us to get the hash code of the value, if Long value is present, otherwise 0 (zero) if no Long value is present in OptionalLong object. Syntax:

public int hashCode()

Parameters: This method 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




// Java program to demonstrate
// OptionalLong.hashCode() method
 
import java.util.OptionalLong;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create a OptionalLong instance
        OptionalLong opLong
            = OptionalLong.of(253255);
 
        System.out.println("OptionalLong: "
                           + opLong.toString());
 
        // get hashCode value using hashCode()
        System.out.println("HashCode value: "
                           + opLong.hashCode());
    }
}


Output:

OptionalLong: OptionalLong[253255]
HashCode value: 253255

Program 2: 

Java




// Java program to demonstrate
// OptionalLong.hashCode() method
 
import java.util.OptionalLong;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create a OptionalLong instance
        OptionalLong opLong
            = OptionalLong.empty();
 
        System.out.println("OptionalLong: "
                           + opLong.toString());
 
        // get hashCode value using hashCode()
        System.out.println("HashCode value: "
                           + opLong.hashCode());
    }
}


Output:

OptionalLong: OptionalLong.empty
HashCode value: 0

References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalLong.html#hashCode()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32477 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6916 POSTS0 COMMENTS