1.字符串判断是否为“” 和null
(1)str == null; (2)"".equals(str); (3)str.length <= 0; (4)str.isEmpty(); or StringUtils.isNullOrEmpty(userId);
2.listl类型
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
CollectionUtils.isNotEmpty(list)
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
建议使用的判断方法:
1.字符串的判断
可以使用commons-lang-x.x.jar或commons-lang3-x.x.jar包下的StringUtils工具类
if( StringUtils.isEmpty() )
if( StringUtils.isNotEmpty() )
2.使用ColletionUtils工具类对List、Set判空
可以使用commons-collections-x.x.x.jar包下的CollectionUtils工具类
if( list.isEmpty() )
if( list.isNotEmpty() )