Java中判断list为空(CollectionUtils.isEmpty)
Java中判断list为空(CollectionUtils.isEmpty)
https://blog.csdn.net/zhuoya_/article/details/104621126
- @Test
- public void testStr() {
- /*----------------【Start】isEmpty方法检查-----------------*/
- List<String> strList1 = Lists.newArrayList();
- System.out.println(CollectionUtils.isEmpty(strList1));//true
- System.out.println(CollectionUtils.isEmpty(new ArrayList()));//true
- System.out.println(strList1 == null);//false
- System.out.println(strList1.size());//0
-
- List<String> strList2 = new ArrayList<>();
- System.out.println(strList2 == null);//false
- strList2=null;
- System.out.println(strList2 == null);//true
- System.out.println(CollectionUtils.isEmpty(strList2));//true
- /*----------------【END】isEmpty方法检查-----------------*/
- }
import org.springframework.util.CollectionUtils;
总结:
1:刚创建出来的List长度为0,但不为null
2:CollectionUtils.isEmpty()方法可以检查null和长度为0的list