Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the OptionalInt.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.

The orElseGet method will return the integer value present in the OptionalInt object. If the value is not present, then the IntSupplier function passed as an argument is executed, and the integer value returned from the function is returned.

In Java, the OptionalInt object is a container object that may or may not contain an integer value.

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

Syntax

public int orElseGet(IntSupplier other)

Parameters

The argument is that the IntSupplier function is to be executed if the OptionalInt object is emptyThere is no value present in the OptionalInt object.

Return value

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

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

Code

The code below shows how to use the orElseGet method.

import java.util.OptionalInt;
class OptionalIntOrElseGetExample {
public static void main(String[] args) {
OptionalInt optional1 = OptionalInt.of(1);
System.out.println("Optional1 : " + optional1);
int val = optional1.orElseGet(()-> 10);
System.out.println("Int Value at Optional1 is : " + val);
OptionalInt optional2 = OptionalInt.empty();
System.out.println("\nOptional2 : " + optional2);
val = optional2.orElseGet(()-> 10);
System.out.println("Int Value at Optional2 is : " + val);
}
}

Explanation

In the code above:

  • In line 1, we imported the OptionalInt class.
import java.util.OptionalInt;
  • In line 5, we created an OptionalInt object with int value 1 using the of method.
OptionalInt optional1 = OptionalInt.of(1);
  • In line 7, we called the orElseGet method on the optional1 object. For the orElseGet method, a function that returns 10 is passed as an argument. The orElseGet method returns 1 because the optional1 object contains the value.
int val =  optional1.orElseGet(()->10);
val;// 1
  • In line 10, we used the empty method to get an empty OptionalInt object. The returned object doesn’t have any value.
OptionalInt optional2 = OptionalInt.empty();
  • In line 12, we called the orElseGet method on the optional2 object. For the orElseGet method, a function which returns 10 is passed as an argument. This method returns 10 because the optional2 object doesn’t contain any value.
val = optional2.orElseGet(()->10);
val;// 10

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