🎓 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 ifPresentOrElse() method in Java programming and how to use it with an example.
1. Optional ifPresentOrElse() Method Overview
Definition:
The ifPresentOrElse() method of the Optional class in Java performs a given action if the value is present; otherwise, performs a different action or method. This method is useful for handling both present and absent cases without using explicit isPresent() checks.
Syntax:
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
Parameters:
- action: The action to be performed if the value is present.
- emptyAction: The action to be performed if the value is not present.
Key Points:
- It was introduced in Java 9.
- This method does not return a new Optional; it is a terminal operation.
- It takes two parameters, a Consumer to operate on the value inside Optional if it is present, and a Runnable to execute if the Optional is empty.
2. Optional ifPresentOrElse() Method Example
import java.util.Optional;
public class OptionalIfPresentOrElseExample {
public static void main(String[] args) {
// Creating an Optional object with a value
Optional<String> valuePresent = Optional.of("Hello World!");
// Using ifPresentOrElse() when value is present
valuePresent.ifPresentOrElse(
v -> System.out.println("Value is present, it's: " + v),
() -> System.out.println("Value is absent!")
);
// Creating an empty Optional object
Optional<String> valueAbsent = Optional.empty();
// Using ifPresentOrElse() when value is absent
valueAbsent.ifPresentOrElse(
v -> System.out.println("Value is present, it's: " + v),
() -> System.out.println("Value is absent!")
);
}
}
Output:
Value is present, it's: Hello World! Value is absent!
Explanation:
In the example, we have two Optional objects – valuePresent which holds a String value, and valueAbsent which is empty.
We use ifPresentOrElse() on both objects. For valuePresent, the lambda expression in the Consumer parameter is executed, printing the present value. For valueAbsent, the Runnable lambda expression is executed, indicating that the value is absent. This showcases how ifPresentOrElse() can handle both cases elegantly.
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