Java高级个人笔记(判断List、Map、Set是否为空及效率比较)

//如果object为null,则设置为defaultValue
ObjectUtils.defaultIfNull(object, defaultValue);

//判断集合是否为null
List list=new ArrayList();
System.out.println(list.isEmpty()); //true
System.out.println(list.size()); //0

Set set=new HashSet();
System.out.println(set.isEmpty()); //true
System.out.println(set.size()); //0

Map map=new HashMap();
System.out.println(map.isEmpty()); //true
System.out.println(map.size()); //0

方法一(数据量大,效率低):
if(list!=null && list.size()>0){
}

方法二(数据量大,效率高):
if(list!=null && !list.isEmpty()){
}

你可能感兴趣的:(Java高级个人笔记(判断List、Map、Set是否为空及效率比较))