CollectionUtils isEmpty() Example in Java

In this post, we will see Apache Commons CollectionUtils isEmpty() Example in Java.

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.netsurfingzone.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.netsurfingzone.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.netsurfingzone.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.


Apache Commons CollectionUtil docs.