java中的不为空判断

String不为空判断

if(null != str && !"".equals(str))

 

List不为空判断

if(list!=null && !list.isEmpty())

 

对象判断不为空

if(null!=user)

  

对象属性判断为空

if(null==user.getName()||"".equals(user.getName())

 

下面这个也可以判断list是否为空

public static boolean isNullOrEmptyList(List list) {
		if (isNullObject(list) || list.size() == 0) {
			return true;
		}

		return false;
	}

/**
	 * 是否为空对象
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNullObject(Object obj) {
		return null == obj;
	}

 

下面这种方法可以判断对象和String是否为空

public boolean isNull(Object obj) {
return obj == null ? true : false;
}
public boolean isEmpty(String s) {
if (isNull(s)) {
return true;
}
return s.trim().length() < 1 ? true : false;
}
public boolean isNotEmpty(String s) {
return !this.isEmpty(s);
}

  

isEmpty()判断有没有元素
而size()返回有几个元素

list.isEmpty()和list.size()==0 没有区别

list!=null跟!list.isEmpty()有什么区别?

这就相当与,你要喝水,
前面就是判断是不是连水杯都没有,
后面就是判断水杯里面没有水,
连盛水的东西都没有,

  


这个水从何而来?
所以一般的判断是
if(list!=null && !list.isEmpty()){
这个里面取list中的值
}else{
做其他处理
}

posted @ 2016-06-29 21:17  Lost blog  阅读(1596)  评论(0编辑  收藏  举报