gpt4 book ai didi

java - CollectionUtils.isEmpty(list) 如何工作?

转载 作者:行者123 更新时间:2023-12-02 04:53:14 27 4
gpt4 key购买 nike

List<String> list = null;  // line1
List<String> list = Collections.emptyList(); // line2

if(CollectionUtils.isEmpty(list)) { // line3
System.out.println("empty"); // line4
} else {
list.forEach(value -> System.out.println(value)); // line5
}

如果我按照第 2 行初始化列表,为什么 if 检查将输出打印为空?但我将检查更改为 list == null,它工作正常。即代码在空列表上迭代。

最佳答案

Java文档:

public static boolean isEmpty(Collection<?> coll)

Null-safe check if the specified collection is empty.

Null returns true.

实现:

return coll == null || coll.isEmpty();

关于java - CollectionUtils.isEmpty(list) 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56423273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com