This post will discuss how to check if a Collection (Set, List, Map, etc.) is empty in Java.

1. Using isEmpty() method

The standard solution to check if a Java Collection is empty is calling the isEmpty() method on the corresponding collection. It returns true if the collection contains no elements.

The following solution provides the custom implementation of isEmpty() and isNotEmpty() methods, that handles null input gracefully.

Download  Run Code

Output:

false

2. Using Apache Commons Collections

Another approach is to use the CollectionUtils.isEmpty() method provided by Apache Commons Collections, which is the null-safe wrapper over the Collection.isEmpty() method.

Download Code

Output:

false

3. Using Apache Commons Lang

Apache Commons Lang provides ObjectUtils.isEmpty() method that returns true if an Object is empty or null; false otherwise.

Download Code

Output:

false

 
The ObjectUtils.isEmpty() method is implemented as follows:

That’s all about checking if a Collection is empty in Java.