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)
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);
}
}
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)
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);
}
}