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? But I change the check to list == null
, it works fine. ie the code iterates on the empty list.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.