🎓 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
null or empty is a common task to ensure the robustness of your code. Depending on the type of object, the methods to check for null or empty values may differ. This guide will cover various ways to check if different types of objects (like String, Collection, Map, and custom objects) are null or empty.Table of Contents
- Introduction
- Checking If a String Is Null or Empty
- Checking If a Collection Is Null or Empty
- Checking If a Map Is Null or Empty
- Checking If a Custom Object Is Null or Empty
- Conclusion
Introduction
Different types of objects in Java require different approaches to check for null or empty values. It's important to handle these checks correctly to avoid NullPointerExceptions and ensure your code handles all cases appropriately.
Checking If a String Is Null or Empty
Example
public class NullOrEmptyCheckExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = "Hello";
System.out.println(isNullOrEmpty(str1)); // true
System.out.println(isNullOrEmpty(str2)); // true
System.out.println(isNullOrEmpty(str3)); // false
}
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
}
Explanation
str == null: Checks if the string isnull.str.isEmpty(): Checks if the string is empty.
Checking If a Collection Is Null or Empty
Example
import java.util.ArrayList;
import java.util.Collection;
public class NullOrEmptyCheckExample {
public static void main(String[] args) {
Collection<String> collection1 = null;
Collection<String> collection2 = new ArrayList<>();
Collection<String> collection3 = new ArrayList<>();
collection3.add("Hello");
System.out.println(isNullOrEmpty(collection1)); // true
System.out.println(isNullOrEmpty(collection2)); // true
System.out.println(isNullOrEmpty(collection3)); // false
}
public static boolean isNullOrEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
}
Explanation
collection == null: Checks if the collection isnull.collection.isEmpty(): Checks if the collection is empty.
Checking If a Map Is Null or Empty
Example
import java.util.HashMap;
import java.util.Map;
public class NullOrEmptyCheckExample {
public static void main(String[] args) {
Map<String, String> map1 = null;
Map<String, String> map2 = new HashMap<>();
Map<String, String> map3 = new HashMap<>();
map3.put("key", "value");
System.out.println(isNullOrEmpty(map1)); // true
System.out.println(isNullOrEmpty(map2)); // true
System.out.println(isNullOrEmpty(map3)); // false
}
public static boolean isNullOrEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
}
Explanation
map == null: Checks if the map isnull.map.isEmpty(): Checks if the map is empty.
Checking If a Custom Object Is Null or Empty
For custom objects, you need to define what "empty" means. This often involves checking if certain fields are null or empty.
Example
public class NullOrEmptyCheckExample {
public static void main(String[] args) {
Person person1 = null;
Person person2 = new Person(null, null);
Person person3 = new Person("John", "Doe");
System.out.println(isNullOrEmpty(person1)); // true
System.out.println(isNullOrEmpty(person2)); // true
System.out.println(isNullOrEmpty(person3)); // false
}
public static boolean isNullOrEmpty(Person person) {
return person == null || (person.getFirstName() == null && person.getLastName() == null);
}
}
class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Explanation
person == null: Checks if the person object isnull.(person.getFirstName() == null && person.getLastName() == null): Checks if both first name and last name fields arenull.
Conclusion
Checking if an object is null or empty in Java can be accomplished using various methods tailored to different types of objects:
- For
String, usestr == null || str.isEmpty(). - For
Collection, usecollection == null || collection.isEmpty(). - For
Map, usemap == null || map.isEmpty(). - For custom objects, define "empty" and check relevant fields.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with different types of objects in Java.
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