判断List、Map、Set是否为空及效率比较
    


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

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

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

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

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

方法二(数据量大,效率高):
if(list!=null && !list.isEmpty()){
}
判断List、Map、Set是否为空及效率比较
    


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

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

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

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

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

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

相关文章:

  • 2022-01-01
  • 2021-11-06
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
猜你喜欢
  • 2021-06-11
  • 2022-12-23
  • 2022-02-06
  • 2022-01-24
  • 2022-01-06
  • 2021-10-07
相关资源
相似解决方案