编程语言
首页 > 编程语言> > Java中判断list为空(CollectionUtils.isEmpty)

Java中判断list为空(CollectionUtils.isEmpty)

作者:互联网

Java中判断list为空(CollectionUtils.isEmpty)
https://blog.csdn.net/zhuoya_/article/details/104621126

  1. @Test
  2. public void testStr() {
  3. /*----------------【Start】isEmpty方法检查-----------------*/
  4. List<String> strList1 = Lists.newArrayList();
  5. System.out.println(CollectionUtils.isEmpty(strList1));//true
  6. System.out.println(CollectionUtils.isEmpty(new ArrayList()));//true
  7. System.out.println(strList1 == null);//false
  8. System.out.println(strList1.size());//0
  9. List<String> strList2 = new ArrayList<>();
  10. System.out.println(strList2 == null);//false
  11. strList2=null;
  12. System.out.println(strList2 == null);//true
  13. System.out.println(CollectionUtils.isEmpty(strList2));//true
  14. /*----------------【END】isEmpty方法检查-----------------*/
  15. }
import org.springframework.util.CollectionUtils;

总结:

1:刚创建出来的List长度为0,但不为null

2:CollectionUtils.isEmpty()方法可以检查null和长度为0的list

标签:Java,list,CollectionUtils,System,isEmpty,println,null,out
来源: https://www.cnblogs.com/sunny3158/p/16033305.html