
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?
In this article, we will learn about the differences between Optional.ifPresentOrElse() and Optional.or() methods in Java 9. Both Optional.ifPresentOrElse() and Optional.or() methods were introduced in Java 9 to improve its functionality.
Optional Class
Optional Class is a part of java.util package, and it was introduced in Java 8. It is a container object that may or may not contain a non-null value. It helps in writing code without using too many null checks.
The following are some of the common methods of the Optional Class:
- empty(): This method returns an empty Optional instance.
- equals(): This method checks whether the other object is "equal to" this Optional.
- get(): If a value is present, it returns the value, else it throws a NoSuchElementException error.
- ifPresent(): If a value is present, calls the specified consumer with the value, else does nothing.
Creating an empty string using the Optional Class so that it will not throw the NullPointerException error:
Optional empty = Optional.empty();
Optional.ifPresentOrElse() Method
The ifPresentOrElse() is a method of the Optional class of Java. The ifPresentOrElse() method checks if the value is present, applies the action with the value, and else returns an empty. The Optional.ifPresentOrElse() method contains two parameters.
This method returns nothing and throws a NullPointerException error if a value is present and the given action is null, or no value is present and the given empty-based action is null.
This method implements the Consumer interface and an implementation of the Runnable interface.
Syntax
The following is the syntax of the ifPresentOrElse() method declaration:
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
Parameters
-
action: The action which has to be performed on this Optional, if a value is present.
- emptyAction: The empty-based action that has to be performed if no value is present.
Example
Below is an example of the Optional.ifPresentOrElse() method in Java:
import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; public class IfPresentOrElseMethodTest { public static void main(String args[]) { optionalIsPresent(); optionalIsEmpty(); } private static void optionalIsPresent() { Optional<String> optional = Optional.of("Test"); AtomicInteger successCounter = new AtomicInteger(); AtomicInteger emptyOptionalsCounter = new AtomicInteger(); optional.ifPresentOrElse(x -> successCounter.incrementAndGet(), () ->emptyOptionalsCounter.incrementAndGet()); System.out.println(successCounter.get()); System.out.println(emptyOptionalsCounter.get()); } private static void optionalIsEmpty() { Optional optional = Optional.empty(); AtomicInteger successCounter = new AtomicInteger(); AtomicInteger emptyOptionalsCounter = new AtomicInteger(); optional.ifPresentOrElse(x -> successCounter.incrementAndGet(), () -> emptyOptionalsCounter.incrementAndGet()); System.out.println(successCounter.get()); System.out.println(emptyOptionalsCounter.get()); } }
The output of the above example is:
1 0 0 1
Optional.or() Method
The Optional.or() is a method of the Optional class of Java. The Optional.or() method checks if the value is present, returns an option that contains a value, else returns an Optional that applies to the Supplier function.
This method returns an Optional and the value of this Optional, if a value is present, else an Optional produced by the supplying function is returned. The Optional.or() method contains only one parameter, Supplier.
This method throws a NullPointerException error if the supplying function is null or produces a null result.
Syntax
The following is the syntax of the Optional.or() method declaration:
public Optional<T> or(Supplier<T> supplier)
Parameter
- supplier: The supplying function that produces an Optional to be returned.
Example
Below is an example of the Optional.or() method in Java:
import java.util.Optional; public class OptionalOrMethodTest { public static void main(String args[]) { optionalIsPresent(); optionalIsEmpty(); } private static void optionalIsPresent() { Optional<String> optional1 = Optional.of("Test1"); Optional<String> optional2 = Optional.of("Test2"); Optional<String> optional3 = optional1.or(() -> optional2); System.out.println(optional3.get()); } private static void optionalIsEmpty() { Optional<String> optional1 = Optional.empty(); Optional<String> optional2 = Optional.of("Test2"); Optional<String> optional3 = optional1.or(() -> optional2); System.out.println(optional3.get()); } }
The output of the above example is:
Test1 Test2
Difference between Optional.ifPresentOrElse() and Optional.or() Methods
The following are the key differences between Optional.ifPresentOrElse() and Optional.or() methods in Java:
Criteria | ifPresentOrElse() | or() |
---|---|---|
Purpose | Executes one action if the value is present, another if not | Returns the current Optional or an alternative |
Return type | void | Optional<T> |
Parameter | Consumer and Runnable | Supplier |
When value present | Runs Consumer | Returns the current Optional |
When value absent | Runs Runnable | Returns the supplier's Optional |