In this short article, we will discuss how to check if the collection is empty or null in Java.
Let's create a standard utility method to check if the collection is empty or null in Java.
Let's create a standard utility method to check if the collection is empty or null in Java.
Learn Java collections framework in-depth at Java Collections Framework in Depth
Check if Collection is Empty or Null in Java - Utility Methods
- isEmptyOrNull(Collection<?> collection) - Return true if the supplied Collection is null or empty. Otherwise, return false.
- isNotEmptyOrNull(Collection<?> collection) - Return true if the supplied Collection is not null or not empty. Otherwise, return false.
1. isEmptyOrNull(Collection<?> collection)
Return true if the supplied Collection is null or empty. Otherwise, return false.
public static boolean isEmptyOrNull(Collection < ? > collection) {
return (collection == null || collection.isEmpty());
}
2. isNotEmptyOrNull(Collection<?> collection)
Return true if the supplied Collection is not null or not empty. Otherwise, return false.
public static boolean isNotEmptyOrNull(Collection < ? > collection) {
return !isEmptyOrNull(collection);
}
Complete Example
Here is a complete example to test the above utility methods
package net.javaguides.lang;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class CollectionNullOrEmptyExample {
public static void main(String[] args) {
// return true if collection is null or empty
System.out.println(isEmptyOrNull(null));
System.out.println(isEmptyOrNull(new ArrayList < > ()));
System.out.println(isEmptyOrNull(new LinkedList < > ()));
System.out.println(isEmptyOrNull(new HashSet < > ()));
// return false if collection is not empty or null
System.out.println(isNotEmptyOrNull(new ArrayList < > ()));
System.out.println(isNotEmptyOrNull(new LinkedList < > ()));
System.out.println(isNotEmptyOrNull(new HashSet < > ()));
// return false if collection is not empty or null
List < String > list = new ArrayList < String > ();
list.add("ABC");
System.out.println(isNotEmptyOrNull(list));
List < String > list1 = new LinkedList < > ();
list1.add("ABC");
System.out.println(isNotEmptyOrNull(list1));
Set < String > set = new HashSet < String > ();
set.add("ABC");
System.out.println(isNotEmptyOrNull(set));
}
public static boolean isEmptyOrNull(Collection << ? > collection) {
return (collection == null || collection.isEmpty());
}
public static boolean isNotEmptyOrNull(Collection << ? > collection) {
return !isEmptyOrNull(collection);
}
}
Output:
true
true
true
true
false
false
false
true
true
true
Related Java Utility Methods or Classes
- Check if Map is Null or Empty in Java
- 18 Useful Collections Utility Methods
- Java Custom CollectionUtils Class
- 27 Useful String Utility Methods
- Executors Utility Class in Java
- java.util.Arrays Class
- Java String Utility Class
- java.util.Collections Class
- Top Reflection Utility Methods
- Java Reflection Utility Class
- Java File Utility Class
- Java Zip Utility Class
- Java 8 Date Utility Class
- Top File Utility Methods
Related Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course