關於List,Set,Map判斷是否爲null以及判斷是否存在對應值~隨手筆記

判斷集合是否爲null

List list=new ArrayList();  
System.out.println(list.isEmpty()); //true
System.out.println(list.size());//0

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

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

方法一(數據量大,效率低):if(list!=null&& list.size()>0){
     }
方法二(數據量大,效率高):if(list!=null&& !list.isEmpty()){
     }

判斷是否存在對應值

if(list.contains(companyId)) {
			
		 }
if(map.containsKey("conpanyId")) {
		
	 }
 if(map.containsValue("b688ecfa-1dd9-446e-ac35-9496eceb3a24")) {
		
	 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章