Check if a List is empty in Java
This post will discuss how to check if a list is empty in Java.
1. Using List.isEmpty() method
A simple solution to check if a list is empty in Java is using the List’s isEmpty() method. It returns true if the list contains no elements. To avoid NullPointerException, precede the isEmpty method call with a null check.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Collection; import java.util.List; class Main { public static boolean isEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } public static void main(String[] args) { List<Integer> nums = List.of(); boolean isEmpty = isEmpty(nums); if (isEmpty) { System.out.println("The collection is empty"); } else { System.out.println("The collection is not empty"); } } } |
Output:
The collection is empty
If you want to do the opposite, i.e., check if the list is not empty, you can use the following expression:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = List.of(); boolean isNotEmpty = nums != null && !nums.isEmpty(); if (isNotEmpty) { System.out.println("The collection is not empty"); } else { System.out.println("The collection is empty"); } } } |
Output:
The collection is empty
2. Using Apache Commons Collections
To get a null-safe version of the isEmpty() method, you may want to use the CollectionUtils.isEmpty() method by Apache Commons Collections.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.collections4.CollectionUtils; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = List.of(); boolean isEmpty = CollectionUtils.isEmpty(nums); if (isEmpty) { System.out.println("The collection is empty"); } else { System.out.println("The collection is not empty"); } } } |
Output:
The collection is empty
3. Check empty or null in List of Lists
To check for an empty list or a null value in a List of Lists, you can use Stream API. For instance,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Arrays; import java.util.List; class Main { public static boolean isAnyEmpty(List<List<Integer>> listOfLists) { return listOfLists.stream() .anyMatch(x -> x == null || x.isEmpty()); } public static void main(String[] args) { List<List<Integer>> listOfLists = Arrays.asList( List.of(1, 2, 3), null, List.of(4, 5) ); boolean isAnyEmpty = isAnyEmpty(listOfLists); if (isAnyEmpty) { System.out.println("The collection is empty"); } else { System.out.println("The collection is not empty"); } } } |
Output:
The collection is empty
Another alternative is to use the contains() method that checks for the specified value in the list. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { public static boolean isAnyEmpty(List<List<Integer>> listOfLists) { return listOfLists.contains(null) || listOfLists.contains(Collections.emptyList()); } public static void main(String[] args) { List<List<Integer>> listOfLists = Arrays.asList( List.of(1, 2, 3), List.of(), List.of(4, 5) ); boolean isAnyEmpty = isAnyEmpty(listOfLists); if (isAnyEmpty) { System.out.println("The collection is empty"); } else { System.out.println("The collection is not empty"); } } } |
Output:
The collection is empty
That’s all about checking if a list is empty in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)