🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Optional or() method in Java programming and how to use it with an example.
1. Optional or() Method Overview
Definition:
The or() method of the Optional class in Java returns an Optional describing the value of the current Optional, if a value is present; otherwise, returns an Optional produced by the supplying function.
Syntax:
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Parameters:
- supplier: The supplier function that produces an Optional to be returned if the original Optional is empty.
Key Points:
- Introduced in Java 9.
- This method is used to provide a default or alternative Optional if the original Optional is empty.
- The supplying function is only invoked if the original Optional is empty.
- If the supplying function produces a null, a NullPointerException is thrown.
2. Optional or() Method Example
import java.util.Optional;
public class OptionalOrExample {
public static void main(String[] args) {
// Creating an Optional object with a value
Optional<String> valuePresent = Optional.of("Hello World!");
// Using or() when value is present
Optional<String> result1 = valuePresent.or(() -> Optional.of("Default Value"));
System.out.println(result1.get()); // Should print: Hello World!
// Creating an empty Optional object
Optional<String> valueAbsent = Optional.empty();
// Using or() when value is absent
Optional<String> result2 = valueAbsent.or(() -> Optional.of("Default Value"));
System.out.println(result2.get()); // Should print: Default Value
}
}
Output:
Hello World! Default Value
Explanation:
In the example, we have two Optional objects – valuePresent which holds a String value, and valueAbsent which is empty.
We use the or() method on both objects. For valuePresent, the original Optional is returned as it has a value. For valueAbsent, the supplying function is invoked, creating a new Optional with the default value, showcasing how or() can be used to provide an alternative Optional in case the original one is empty.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment