sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

posted on 2022-03-21 09:28  sunny123456  阅读(4831)  评论(1编辑  收藏  举报