Apache Commons Collections - Safe Empty Checks



Checking non-empty list

isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.

Usage

CollectionUtils.isNotEmpty(list)

Declaration

Following is the declaration for

org.apache.commons.collections4.CollectionUtils.isNotEmpty() method −

public static boolean isNotEmpty(Collection<?> coll)

Parameters

  • coll − The collection to check, may be null.

Return Value

True if non-null and non-empty.

Example - Checking a List to be non-empty in Safer way

The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isNotEmpty() method. We'll check a list is empty or not.

CommonCollectionsTester.java

package com.tutorialspoint;

import java.util.List;
import org.apache.commons.collections4.CollectionUtils;

public class CommonCollectionsTester {
   public static void main(String[] args) {
      List<String> list = getList();
      System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
      System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
   }
   static List<String> getList() {
      return null;
   }
   static boolean checkNotEmpty1(List<String> list) {
      return !(list == null || list.isEmpty());
   }
   static boolean checkNotEmpty2(List<String> list) {
      return CollectionUtils.isNotEmpty(list);
   }
}

Output

The output is given below −

Non-Empty List Check: false
Non-Empty List Check: false

Checking a List to be empty in Safer way

isEmpty() method of CollectionUtils can be used to check if a list is empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.

Usage

CollectionUtils.isEmpty(list);

Declaration

Following is the declaration for

org.apache.commons.collections4.CollectionUtils.isEmpty() method −

public static boolean isEmpty(Collection<?> coll)

Parameters

  • coll − The collection to check, may be null.

Return Value

True if empty or null.

Example - Checking a List to be empty

The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isEmpty() method. We'll check a list is empty or not.

CommonCollectionsTester.java

package com.tutorialspoint;

import java.util.List;
import org.apache.commons.collections4.CollectionUtils;

public class CommonCollectionsTester {
   public static void main(String[] args) {
      List<String> list = getList();
      System.out.println("Empty List Check: " + checkEmpty1(list));
      System.out.println("Empty List Check: " + checkEmpty1(list));
   }
   static List<String> getList() {
      return null;
   }
   static boolean checkEmpty1(List<String> list) {
      return (list == null || list.isEmpty());
   }
   static boolean checkEmpty2(List<String> list) {
      return CollectionUtils.isEmpty(list);
   }
}

Output

Given below is the output of the code −

Empty List Check: true
Empty List Check: true
Advertisements