English 中文(简体)
Commons Collections - Safe Empty Checks
  • 时间:2024-11-05

Commons Collections - Safe Empty Checks


Previous Page Next Page  

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

Checking non-empty pst

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

Declaration

Following is the declaration for

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


pubpc 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 pst is empty or not.


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

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

Output

The output is given below −


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

Checking empty pst

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

Declaration

Following is the declaration for

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


pubpc 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 pst is empty or not.


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

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

Output

Given below is the output of the code −


Empty List Check: true
Empty List Check: true
Advertisements