Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the OptionalLong.orElseGet method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In Java, the OptionalLong object is a container object that may or may not contain a long value.

The OptionalLong class is present in the java.util package.

What is the orElseGet method of the OptionalLong class?

The orElseGet() method will return the long value present in the OptionalLong object.

If the value is not present, then the LongSupplier function passed as an argument is executed and the long value returned from the function is returned.

Syntax


public long orElseGet(LongSupplier other)

Argument

The argument of the LongSupplier function is to be executed if the OptionalLong object is emptyno value is present in the OptionalLong object.

Return value

If the OptionalLong object contains a value, then the value is returned. Otherwise, the value from the passed argument function is returned.


This method throws NullPointerException if no value is present and the LongSupplier function is null.

Code

The code below shows how to use the orElseGet method.

import java.util.OptionalLong;
class OptionalLongOrElseGetExample {
public static void main(String[] args) {
OptionalLong optional1 = OptionalLong.of(1);
System.out.println("Optional1 : " + optional1);
long val = optional1.orElseGet(()-> 2147483647l);
System.out.println("Long Value at Optional1 is : " + val);
OptionalLong optional2 = OptionalLong.empty();
System.out.println("\nOptional2 : " + optional2);
val = optional2.orElseGet(()-> 2147483647l);
System.out.println("Long Value at Optional2 is : " + val);
}
}

Explanation

In the code above:

  • Line 1: We imported the OptionalLong class.


    import java.util.OptionalLong;
    

  • Line 4: We created an OptionalLong object with long value 1 using the of method.


    OptionalLong optional1 = OptionalLong.of(1);
    

  • Line 6: We called the orElseGet method on the optional1 object.

    For the orElseGet method a function which returns 2147483647l is passed as an argument.

    The orElseGet method returns 1 because the optional1 object contains the value.


    long val =  optional1.orElseGet(()-> 2147483647l); 
    val; // 1
    

  • Line 9: We used the empty method to get an empty OptionalLong object. The returned object doesn’t have any value.


    OptionalLong optional2 = OptionalLong.empty();
    

  • Line 11: We called the orElseGet method on the optional2 object.

    For the orElseGet method a function which returns 2147483647l is passed as an argument.

    This method returns 2147483647l because the optional2 object doesn’t contain any value.


    val = optional2.orElseGet(()-> 2147483647l);
    val; // 2147483647
    

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring