Commons Collections 简明教程

Commons Collections - Safe Empty Checks

Apache Commons Collections 库的 CollectionUtils 类提供各种实用方法,用于涵盖广泛使用场景的常见操作。它有助于避免编写样板代码。在 jdk 8 之前,该库非常有用,因为 Java 8 的 Stream API 中现在提供了类似的功能。

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

CollectionUtils 的 isNotEmpty() 方法可用于检查一个列表是否不为空,而无需担心列表为 null。因此,在检查列表大小之前不必到处放置 null 检查。

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() 方法 -

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

public static boolean isNotEmpty(Collection<?> coll)

Parameters

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

Return Value

如果非 null 且非空,则为 true。

True if non-null and non-empty.

Example

以下示例显示了 org.apache.commons.collections4.CollectionUtils.isNotEmpty() 方法的用法。我们将检查一个列表是否为空。

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

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

public class CollectionUtilsTester {
   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 empty list

CollectionUtils 的 isEmpty() 方法可用于检查一个列表是否为空,而无需担心列表为 null。因此,在检查列表大小之前不必到处放置 null 检查。

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() 方法 -

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

public static boolean isEmpty(Collection<?> coll)

Parameters

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

Return Value

如果为空或为 null 则为 true。

True if empty or null.

Example

以下示例显示了 org.apache.commons.collections4.CollectionUtils.isEmpty() 方法的用法。我们将检查一个列表是否为空。

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

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

public class CollectionUtilsTester {
   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