zoukankan      html  css  js  c++  java
  • 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{
    做其他处理
    }

  • 相关阅读:
    (16)C#继承
    (3)新概念英语一(11-20)lessons
    (15)C#集合
    (14)编码
    (2)新概念英语一(1-10)lessons
    (13)C#数组和元组
    Uva 11729 Commando War
    Uva 11292 Dragon of Loowater
    谈谈单元最短路
    [USACO 3.3.1]骑马修栅栏t
  • 原文地址:https://www.cnblogs.com/JAYIT/p/5628288.html
Copyright © 2011-2022 走看看