寫代碼時候經(jīng)常遇到判empty、null和blank的情況。這些概念看起來很相似,但是它們有著不同的含義和用法。本文將介紹這三個概念的區(qū)別,并分析具體場景該如何做判斷。
empty
empty是指一個字符串長度為0/Java集合對象不存在元素,可以使用isempty()方法來判斷一個字符串是否為空。
也可以使用isEmpty()方法來判斷Java集合元素否為空。
例如:
String str = "";
if (str.isEmpty()) {
System.out.println("This string is empty");
}
上述代碼會輸出"This string is empty",因為變量str是一個空字符串。
此外,還有一個常見的錯誤就是認(rèn)為一個字符串包含空格或多個空格就是empty,這是不正確的。例如:
String str = " ";
if (str.isEmpty()) {
System.out.println("This string is empty");
}
上述代碼不會輸出任何內(nèi)容,因為變量str并不是一個空字符串,而是包含兩個空格的字符串。
null
null表示一個對象不存在。如果一個引用變量沒有被初始化或被顯式賦值為null,那么這個變量的值就是null。例如:
String str = null;
if (str == null) {
System.out.println("This string is null");
}
上述代碼會輸出"This string is null",因為變量str的值為null。
需要注意的是,對于基本數(shù)據(jù)類型(如int、double等),它們不能被賦值為null。如果試圖將一個基本數(shù)據(jù)類型賦值為null,會出現(xiàn)編譯錯誤。
blank
blank是指一個字符串長度大于0但是只包含空格(包括制表符和換行符)的情況。Java中可以使用isblank()方法來判斷一個字符串是否為blank。例如:
String str = " ";
if (str.isBlank()) {
System.out.println("This string is blank");
}
上述代碼會輸出"This string is blank",因為變量str包含兩個空格,并且這是這個字符串唯一的內(nèi)容。
需要注意的是,在Java 11之前是沒有isblank()方法的。如果你正在使用早期版本的Java,應(yīng)該使用trim()方法來去掉字符串兩端的空格,并檢查結(jié)果是否為空。例如:
String str = " ";
if (str.trim().isEmpty()) {
System.out.println("This string is blank");
}
上述代碼也會輸出"This string is blank",因為變量str經(jīng)過trim()方法處理后變成了一個空字符串。
總結(jié)一下
empty、null和blank三個概念有著明顯的區(qū)別:
- empty表示一個字符串長度為0,Java集合不存在元素。
- null表示一個對象不存在;
- blank表示一個字符串長度大于0但是只包含空格。
為了更好地理解這些概念和區(qū)別,以下是一些示例代碼。
public class StringExample {
public static void main(String[] args) {
String emptyStr = "";
String nullStr = null;
String blankStr = " ";
// 判斷empty字符串
if (emptyStr.isEmpty()) {
System.out.println("This string is empty");
}
// 判斷null字符串
if (nullStr == null) {
System.out.println("This string is null");
}
// 判斷blank字符串
if (blankStr.isBlank()) {
System.out.println("This string is blank");
}
}
}
上述代碼會輸出"This string is empty"和"This string is blank",因為變量emptyStr是一個空字符串,變量blankStr包含兩個空格。
Java集合如何判空
Java List判空
isEmpty() 方法是一個通用的方法,可以用于所有實現(xiàn)了 java.util.Collection 接口的類(如 List、Set 等等)。如果集合中沒有任何元素,則返回 true;否則返回 false。
List<String> list = new ArrayList<>();
System.out.println(list.isEmpty()); // 輸出 true
list.add("apple");
System.out.println(list.isEmpty()); // 輸出 false
以上代碼創(chuàng)建了一個空的 ArrayList,并使用 isEmpty() 方法檢查它是否為空。然后在列表中添加了一個元素,再次使用 isEmpty() 方法進行檢查。第一次輸出為 true,因為列表中沒有元素;第二次輸出為 false,因為列表中有一個元素。
Java Map判空
Map可以通過以下幾種方式來判斷是否為空:
1. 使用 isEmpty() 方法:Map 提供了一個 isEmpty() 方法,如果 Map 中沒有任何映射關(guān)系,則返回 true。
Map<String, String> map = new HashMap<>();
System.out.println(map.isEmpty()); // 輸出 true
map.put("key", "value");
System.out.println(map.isEmpty()); // 輸出 false
2. 判斷 Map 的大?。菏褂?size() 方法獲取 Map 中鍵值對的數(shù)量,如果為 0,則說明 Map 為空。
Map<String, String> map = new HashMap<>();
System.out.println(map.size() == 0); // 輸出 true
map.put("key", "value");
System.out.println(map.size() == 0); // 輸出 false
3. 判斷 Map 中的鍵集合是否為空:使用 keySet() 方法獲取 Map 中所有鍵的集合,然后判斷集合是否為空。
Map<String, String> map = new HashMap<>();
System.out.println(map.keySet().isEmpty()); // 輸出 true
map.put("key", "value");
System.out.println(map.keySet().isEmpty()); // 輸出 false
需要注意的是,以上三種方法都可以判斷 Map 是否為空,但建議根據(jù)實際情況選擇最適合的方法。如果只需要知道 Map 是否為空,直接使用 isEmpty() 方法;如果需要執(zhí)行其他操作(如遍歷、刪除等),則可能需要獲取 Map 的大小或鍵集合。文章來源:http://www.zghlxwxcb.cn/news/detail-459812.html
小伙伴們,你學(xué)廢了嗎??文章來源地址http://www.zghlxwxcb.cn/news/detail-459812.html
到了這里,關(guān)于Java empty、null、blank 還傻傻分不清楚?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!