The emptyCollection()
method is not part of the standard Java API and there’s no CollectionUtils class in Java SE (Standard Edition). However, there is a class called CollectionUtils in Apache Commons Collections, which provides various utility methods for working with collections. Here’s an example of how you can use the emptyCollection()
method from Apache Commons Collections. We will see how to use CollectionUtils emptyCollection()
method.
Note – Note that since version 4.0, Apache Commons Collections is not been actively maintained and its usage is discouraged in new projects. Java SE provides several alternatives for this kind of functionality, such as Collections.emptyList, Collections.emptySet or Collections.emptyMap methods.
We need to add the below dependency in the maven to use org.apache.commons.lang3.CollectionUtils emptyCollection()
the method. We can download the apache-commons maven dependency from here.
pom.xml changes
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
CollectionUtils emptyCollection() Example in Java
package com.javatute;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Collection;
public class CollectionUtilsEmptyCollectionExample {
public static void main(String[] args) {
Collection<String> newList = CollectionUtils.emptyCollection();
System.out.println(newList.size());
System.out.println(newList);
}
}
Output is
0
[]
That’s all about CollectionUtils emptyCollection() Example in Java.
When to use CollectionUtils emptyCollection() method.
The emptyCollection()
is a method in the Apache Commons Collection library’s CollectionUtils
class. It returns an empty collection of a specified type. This can be useful in situations where you need to return an empty collection, but the type of collection required isn’t known until runtime.
For example, if you have a method that returns a collection, you can use emptyCollection()
to return an empty collection if no elements are found.
public Collection<String> getElements() {
Collection<String> elements = ...;
if (elements.isEmpty()) {
return CollectionUtils.emptyCollection();
}
return elements;
}
An alternative of CollectionUtils emptyCollection() method
An alternative to using the emptyCollection() method from the CollectionUtils class of Apache Commons Collection library is to use the Collections.emptyList()
and Collections.emptySet()
methods from the Java Standard Library’s Collections class. These methods return an immutable, empty list or set respectively, which can be used when you need to return an empty collection of a specific type.
public Collection<String> getElements() {
Collection<String> elements = ...;
if (elements.isEmpty()) {
return Collections.emptyList();
}
return elements;
}
See docs
Related post
- CollectionUtils isEmpty() Example in Java.
- StringUtils join() Example in Java.
- CollectionUtils union() Example in Java
- CollectionUtils intersection() Example in Java
- CollectionUtils isEqualCollection() Example in Java
- CollectionUtils permutations() Example in Java
- CollectionUtils retainAll() Example in Java
- CollectionUtils reverseArray() Example in Java