简体   繁体   English

CollectionUtils.isEmpty(list)如何工作?

[英]How does CollectionUtils.isEmpty(list) work?

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
}

If I initialize the list as per line2, why the if check prints the output as empty? 如果我按行2初始化列表,为什么if检查将输出打印为空? But I change the check to list == null , it works fine. 但我将检查更改为list == null ,它工作正常。 ie the code iterates on the empty list. 即代码在空列表上迭代。

JavaDoc: JavaDoc的:

public static boolean isEmpty(Collection<?> coll)

Null-safe check if the specified collection is empty.

Null returns true.

Implemented: 实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM