需要注意使用**isEmpty**
可能会出现空指针异常情况,推荐使用CollectionUtils.isEmpty
public static void main(String[] args) {
/*----------------【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方法检查-----------------*/
List<String> strList3 = Lists.newArrayList();
List<String> strings = new ArrayList<>();
System.out.println("strList3 = " + strList3.isEmpty());//true
System.out.println("strings.isEmpty() = " + strings.isEmpty());//true
strings = null;
System.out.println("strings.isEmpty() null = " + strings.isEmpty());//空指针异常
}
总结
1:刚创建出来的List长度为0,但不为null
2:CollectionUtils.isEmpty()方法可以检查null和长度为0的list