CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8\’s Stream API.

    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.

    Declaration

    Following is the declaration for

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

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

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

    1. import java.util.List;
    2.  
    3. import org.apache.commons.collections4.CollectionUtils;
    4.  
    5. public class CollectionUtilsTester {
    6. public static void main(String[] args) {
    7. List<String> list = getList();
    8. System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
    9. System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
    10. }
    11.  
    12. static List<String> getList() {
    13. return null;
    14. }
    15.  
    16. static boolean checkNotEmpty1(List<String> list) {
    17. return !(list == null || list.isEmpty());
    18. }
    19.  
    20. static boolean checkNotEmpty2(List<String> list) {
    21. return CollectionUtils.isNotEmpty(list);
    22. }
    23. }

    Output

    It will print the following result.

    1. Non-Empty List Check: false
    2. Non-Empty List Check: false

    Checking empty list

    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.

    Declaration

    Following is the declaration for

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

    1. public static boolean isEmpty(Collection<?> coll)

    Parameters

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

    Return Value

    True if empty or null.

    Example

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

    1. import java.util.List;
    2.  
    3. import org.apache.commons.collections4.CollectionUtils;
    4.  
    5. public class CollectionUtilsTester {
    6. public static void main(String[] args) {
    7. List<String> list = getList();
    8. System.out.println("Empty List Check: " + checkEmpty1(list));
    9. System.out.println("Empty List Check: " + checkEmpty1(list));
    10. }
    11.  
    12. static List<String> getList() {
    13. return null;
    14. }
    15.  
    16. static boolean checkEmpty1(List<String> list) {
    17. return (list == null || list.isEmpty());
    18. }
    19.  
    20. static boolean checkEmpty2(List<String> list) {
    21. return CollectionUtils.isEmpty(list);
    22. }
    23. }

    Output

    It will print the following result.

    1. Empty List Check: true
    2. Empty List Check: true