判斷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()){
}
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM