🎓 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 Java development, handling null values effectively is crucial for robust and error-free code. Often, there is a need to retrieve the first non-null value from a set of variables or expressions. This scenario is common in applications with multiple fallbacks or default values. In this blog post, we'll explore various methods to find the first non-null value in Java, ensuring code reliability and readability.
Understanding the Need for Non-Null Values
Finding the first non-null value is essential in scenarios where multiple potential values are available, and you want to use the first available valid one. This situation arises frequently in settings like configurations, user inputs, or data fetched from multiple sources.
Different Ways to Get the First Non-Null Value
Method 1: Traditional Approach using Conditional Checks
The most straightforward method is to use a series of if statements to check each value and return the first non-null one.
public class NonNullFinder {
public static void main(String[] args) {
String a = null;
String b = null;
String c = "Hello, World!";
String firstNonNull = a != null ? a : (b != null ? b : c);
System.out.println("First non-null value: " + firstNonNull);
}
}
Output:
First non-null value: Hello, World!
Method 2: Using Java 8's Optional API
import java.util.Optional;
public class NonNullFinder {
public static void main(String[] args) {
String a = null;
String b = null;
String c = "Hello, World!";
String firstNonNull = Optional.ofNullable(a)
.orElse(Optional.ofNullable(b)
.orElse(c));
System.out.println("First non-null value: " + firstNonNull);
}
}
Output:
First non-null value: Hello, World!This method is more elegant and works well with a small number of variables.
Method 3: Using Apache Commons Lang
import org.apache.commons.lang3.ObjectUtils;
public class NonNullFinder {
public static void main(String[] args) {
String a = null;
String b = null;
String c = "Hello, World!";
String firstNonNull = ObjectUtils.firstNonNull(a, b, c);
System.out.println("First non-null value: " + firstNonNull);
}
}
Output:
First non-null value: Hello, World!Apache Commons Lang provides a clean and concise way to achieve this with minimal code.
Method 4: Using Stream API
import java.util.Arrays;
import java.util.List;
public class NonNullFinder {
public static void main(String[] args) {
List<String> values = Arrays.asList(null, null, "Hello, World!");
String firstNonNull = values.stream()
.filter(java.util.Objects::nonNull)
.findFirst()
.orElse(null);
System.out.println("First non-null value: " + firstNonNull);
}
}
Output:
First non-null value: Hello, World!
Conclusion
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