In this post, we will see Apache Commons CollectionUtils isEmpty() Example in Java.
The org.apache.commons.collections4.CollectionUtils isEmpty() method is used to check any collections(List, Set, etc.) are empty or not. It checks for null as well as size of collections. The CollectionUtils isEmpty() is a static method, which accepts Collection as a parameter.
public static boolean isEmpty(final Collection coll)
The isEmpty() returns true if the size of the collection is zero or if the collection is null. We need to add the following dependency in pom.xml in order to use Apache Commons libraries.
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
CollectionUtils isEmpty() Example to check the list is empty or not.
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CheckListIsEmpty { public static void main(String[] args) { List<String> list = new ArrayList<>(); System.out.println(CollectionUtils.isEmpty(list));// true // add some element list.add("john"); list.add("mark"); System.out.println(CollectionUtils.isEmpty(list));// false //assign null to list list = null; System.out.println(CollectionUtils.isEmpty(list));// true } }
Output is –
true
false
true
CollectionUtils isEmpty() Example to check the set is empty or not.
package com.javatute.serviceimpl; import java.util.HashSet; import java.util.Set; import org.apache.commons.collections4.CollectionUtils; public class CheckListIsEmpty { public static void main(String[] args) { Set<String> set = new HashSet<>(); System.out.println(CollectionUtils.isEmpty(set));// true // add some element set.add("john"); set.add("mark"); System.out.println(CollectionUtils.isEmpty(set));// false // assign null to list set = null; System.out.println(CollectionUtils.isEmpty(set));// true } }
Output is –
true
false
true
The internal implementation of CollectionUtil.isEmpty().
Internally isEmpty() method uses == operator for null check and to check collections is empty internally it uses java.util.Collectioni sEmpty() method.
public static boolean isEmpty(final Collection coll) {
return coll == null || coll.isEmpty();
}
CollectionUtil.isNotEmpty() example in Java.
Let’s see an example of org.apache.commons.collections4.CollectionUtils.isNotEmpty().
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionIsNotEmptyExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); // add some element list.add("john"); list.add("mark"); System.out.println(CollectionUtils.isNotEmpty(list));// true // assign null to list list = null; System.out.println(CollectionUtils.isNotEmpty(list));// true } }
Output is –
true
false
Note – CollectionUtils.isNotEmpty() internally uses isEmpty() method only.
public static boolean isNotEmpty(final Collection<?> coll) {
return !isEmpty(coll);
}
That’s all about CollectionUtils isEmpty() Example in Java.
You may like.
- StringUtils isEmpty() and IsBlank() Example in Java.
- String compareTo() method in java.
- String to int in java
- How to convert Arraylist to array and array to ArrayList in Java
- How to make List, Set and Map Read Only in Java
- How to avoid duplicate elements in ArrayList
- Constructor chaining in java with example
- Static variable, method and block in java
- instance variable in java
- How ArrayList works internally in Java
- Difference between String and StringBuffer in java.
- Java String Programs With examples.
- How HashMap works internally in Java.
- How run() method Works internally in Java.
- Difference between HashMap and Hashtable in java
- Difference between Comparable and Comparator in java
- Difference between Iterator and ListIterator in Java
- Difference between Iterator and Enumeration in java.
- instanceof operator in java.
- String interview questions in java.
- Program to remove duplicate characters from String in Java.
- Program to check two strings are an anagram or not in java.
Apache Commons CollectionUtil docs.