java判斷【數組,集合,字符串】是否為空以及空字符串的比較方法記錄


import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class JudgeEmpty {
    public static void main(String[] args) {
        /*1.判斷數組是否為空?*/

        String[] strArr = new String[]{};

        // 判斷suzu不為null,且素組長度大於0
        if (strArr != null && strArr.length > 0) {
            System.out.println("suzu不為null,且素組長度大於0");
        }

        // 判斷suzu為null或素組長度小於0
        if (strArr == null || strArr.length <= 0) {
            System.out.println("suzu為null或素組長度小於=0");
        }

        /*  2.判斷集合是否為空?*/


        List<String> list = Lists.newArrayList();

        // 判斷集合list是否為空,同時判斷list為null,為空集合
        if (CollectionUtils.isEmpty(list)) {
            System.out.println("集合list是否為空,同時判斷list為null,為空集合");
        }

        // 判斷集合list是否為空,同時判斷list不為null,不為空集合
        if (CollectionUtils.isNotEmpty(list)) {
            System.out.println("集合list是否為空,同時判斷list不為null,不為空集合");
        }

        /*3.判斷字符串是否為空?*/
        String str = null;

        System.out.println("判斷字符串是否為空:" + StringUtils.isNotBlank(str));
        // 判斷string不為"null"、""、" "
        if (StringUtils.isNotBlank(str)) {
        }

        // 判斷string為"null"、""、" "
        if (StringUtils.isBlank(str)) {
        }

        /* 4.判斷兩個字符串是否相等(內容相等)?*/


        String str1 = null;
        String str2 = null;

        System.out.println("判斷兩個字符串是否相等:" + ObjectUtils.equals(str1, str2));

        // 判斷兩個字符串是否相等,此方法可以避免空指針異常
        if (ObjectUtils.equals(str1, str2)) {
           /*  如果 string1 = null && string1 = null 返回true
             如果 string1 = null || string1 = null 返回false*/
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM