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
,效果很好。即代码在空列表上进行迭代。
答案 0 :(得分:0)
JavaDoc:
public static boolean isEmpty(Collection<?> coll)
Null-safe check if the specified collection is empty.
Null returns true.
已实施:
return coll == null || coll.isEmpty();