Java empty、null、blank 还傻傻分不清楚?

写代码时候经常遇到判empty、null和blank的情况。这些概念看起来很相似,但是它们有着不同的含义和用法。本文将介绍这三个概念的区别,并分析具体场景该如何做判断。

empty

empty是指一个字符串长度为0/Java集合对象不存在元素,可以使用isempty()方法来判断一个字符串是否为空。也可以使用isEmpty()方法来判断Java集合元素否为空。

例如:

public class Demo {

    public static void main(String[] args) {

        String str = "";

        if (str.isEmpty()) {

            System.out.println("This string is empty");

        }

    }

}

上述代码会输出"This string is empty",因为变量str是一个空字符串。

此外,还有一个常见的错误就是认为一个字符串包含空格或多个空格就是empty,这是不正确的。例如:

public class Demo {

    public static void main(String[] args) {

        String str = "  ";

        if (str.isEmpty()) {

            System.out.println("This string is empty");

        }

    }

}

上述代码不会输出任何内容,因为变量str并不是一个空字符串,而是包含两个空格的字符串。

null

null表示一个对象不存在。如果一个引用变量没有被初始化或被显式赋值为null,那么这个变量的值就是null。例如:

public class Demo {

    public static void main(String[] args) {

        String str = null;

        if (str == null) {

            System.out.println("This string is null");

        }

    }

}

上述代码会输出"This string is null",因为变量str的值为null。

需要注意的是,对于基本数据类型(如int、double等),它们不能被赋值为null。如果试图将一个基本数据类型赋值为null,会出现编译错误。

blank

blank是指一个字符串长度大于0但是只包含空格(包括制表符和换行符)的情况。Java中可以使用isblank()方法来判断一个字符串是否为blank。例如:

public class Demo {

    public static void main(String[] args) {

        String str = "  ";

        if (str.isBlank()) {

            System.out.println("This string is blank");

        }

    }

}

上述代码会输出"This string is blank",因为变量str包含两个空格,并且这是这个字符串唯一的内容。

需要注意的是,在Java 11之前是没有isblank()方法的。如果你正在使用早期版本的Java,应该使用trim()方法来去掉字符串两端的空格,并检查结果是否为空。例如:

public class Demo {

    public static void main(String[] args) {

        String str = "  ";

        if (str.trim().isEmpty()) {

            System.out.println("This string is blank");

        }

    }

}

上述代码也会输出"This string is blank",因为变量str经过trim()方法处理后变成了一个空字符串。

小结

empty、null和blank三个概念有着明显的区别:

empty表示一个字符串长度为0,Java集合不存在元素。

null表示一个对象不存在;

blank表示一个字符串长度大于0但是只包含空格。

为了更好地理解这些概念和区别,以下是一些示例代码。

public class Demo {
    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()) { // 注意: Java 11之前是没有isblank()方法的
            System.out.println("This string is blank");
        }
    }
}

上述代码会输出"This string is empty"和"This string is blank",因为变量emptyStr是一个空字符串,变量blankStr包含两个空格。

Java集合如何判空

List判空

isEmpty() 方法是一个通用的方法,可以用于所有实现了 java.util.Collection 接口的类(如 List、Set 等)。如果集合中没有任何元素,则返回 true;否则返回 false。

import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        System.out.println(list.isEmpty()); // 输出 true

        list.add("apple");
        System.out.println(list.isEmpty()); // 输出 false
    }
}

以上代码创建了一个空的 ArrayList,并使用 isEmpty() 方法检查它是否为空。然后在列表中添加了一个元素,再次使用 isEmpty() 方法进行检查。第一次输出为 true,因为列表中没有元素;第二次输出为 false,因为列表中有一个元素。

Map判空

Map可以通过以下几种方式来判断是否为空:

1. 使用 isEmpty() 方法:Map 提供了一个 isEmpty() 方法,如果 Map 中没有任何映射关系,则返回 true。

import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) {
        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 中键值对的数量,如果为 0,则说明 Map 为空。

import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) {
        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 中所有键的集合,然后判断集合是否为空。

import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) {
        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 是否为空,但建议根据实际情况选择最适合的方法。如果只需要知道 Map 是否为空,直接使用 isEmpty() 方法;如果需要执行其他操作(如遍历、删除等),则可能需要获取 Map 的大小或键集合。

猜你喜欢

转载自blog.csdn.net/xijinno1/article/details/132123472