字符串、集合如何判断空值?看当作年人的正确操做

在平时的开发中,基本上都会用到字符串判断空值和集合判断空值的处理,还记得在刚干开发的时候,写的代码在如今看起来是真的有点Hello World,那么此次分享两个很是经常使用的方法,字符串非空判断和集合非空判断程序员

字符串非空判断

你有没见过下面的代码,要是没见过你就不配是一个程序员,我还写过呢!如今回过头来看感受本身当年真的是太年轻了。apache

public static void main(String[] args) {

    String str = "bingfeng";

    if (str == null || str.equals("")) {

    }
}
复制代码

那么经历同事的各类嫌弃以后,如今终于不这么写了,要是如今看到那个初级工程师这么写,确定叫兄弟过去欺负他。安全

除了这种写法以外,也见到过有些人愿意本身去实现封装一层,写一个工具类用,其实真的感受不必,切莫重复早轮子。这种东西别人已经帮咱们作好了,并且也比咱们这些菜鸟作的好多了,因此推荐直接用就好了。app

咱们直接引入pom文件便可,他给咱们提供了一些基础的功能供咱们使用。工具

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>
复制代码

首先第一种,isNotEmpty 这个方法能够判断字符串是否为空。 第二种,isNotBlank 这个方法也是用来判断字符串是否为空。code

public static void main(String[] args) {

    String str = "bingfeng";

    if (StringUtils.isNotBlank(str)) {

        System.out.println("isNotBlank:" + str);
    }

    if (StringUtils.isNotEmpty(str)) {

        System.out.println("isNotEmpty:" + str);
    }
}
复制代码

既然上面两种方法均可以判断时候为空,那又有什么区别呢?首先两个方法均可以判断字符串是否为null,可是咱们日常在业务中,特别是用户搜索,用户极可能输入空白字符,若是用户什么也没输入,就敲了两个空格,那么提交到后台,按道理来讲空字符串确定是不合法的,那么此时的isNotEmpty是没法判断的,相反isNotBlank却能够在去除字符串两边的空格而后再进行判断,因此这里推荐你们使用 isNotBlank 更为安全。ip

集合空值判断

再来看一段当年的传奇之做开发

public static void main(String[] args) {

    List<String> list = new ArrayList<>();

    if (list == null || list.size() <= 0) {

    }
}
复制代码

通常对集合都要进行两项判断,首先判断是否不为null,其次判断是否不为为空,若是都知足,再进行下面的操做,咱们用上面的写法虽然说没什么问题,可是真的有点太年轻了。字符串

推荐写法:it

public static void main(String[] args) {

    List<String> list = new ArrayList<>();

    if (CollectionUtils.isEmpty(list)) {

    }
}
复制代码

咱们来看下这个方法的底层实现,便会长大许多。

public static boolean isEmpty(@Nullable Collection<?> collection) {
    return collection == null || collection.isEmpty();
}
复制代码

写到这里,基本上就差很少啦,可是仍是透露一下我经常使用的秘籍,我通常都会对判断集合的方式,作一层包装作成一个工具类,提供更多的方法提升代码的复用性。你们且看下面。

/**
 * @Description: TODO <集合工具类>
 * @Date: 2019/10/15/015  09:32
 * @Author: bingfeng
 */
public class ArrayUtil {

    /**
     * 判断集合是否为空
     *
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection<?> collection) {
        return CollectionUtils.isEmpty(collection);
    }

    /**
     * 将集合中的元素输出为字符串,并用{@code separator}链接
     *
     * @param collection
     * @param separator
     * @return
     */
    public static String join(Collection<?> collection, String separator) {

        if (isEmpty(collection)) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (Object o : collection) {
            sb.append(o).append(separator);
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    /**
     * 建立一个空的集合
     *
     * @return
     */
    public static <T> List<T> emptyList() {
        return Collections.emptyList();
    }

    /**
     * 将字符串按特定字符分割
     *
     * @param str
     * @param separator
     * @return
     */
    public static List<String> transition(String str, String separator) {

        if (StringUtils.isBlank(str)) {
            return emptyList();
        }

        String[] split = str.split(separator);

        return Arrays.asList(split);
    }
}
复制代码

道阻且长,就让咱们恩恩爱爱,活得潇潇洒洒吧。。。

相关文章
相关标签/搜索