1. 程式人生 > >Java各種型別判空

Java各種型別判空

1、String型別

(1)str == null;
(2)"".equals(str);
(3)str.length <= 0;
(4)str.isEmpty();
or
StringUtils.isNullOrEmpty(userId);

2、List型別

List<String> list=new ArrayList<String>();
CollectionUtils.isNotEmpty(list); 
或
list != null && !list.isEmpty()//判斷size()效率低
System.out.println(list.isEmpty()); //true
System.out.println(list.size()); //0

3、Map型別

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