zoukankan      html  css  js  c++  java
  • 工具类系列---【如何优雅的对String,List,Map判空?】

    1、导入maven坐标:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
    </dependency>

    2、StringUtils

    //对String类型的字符串进行判空
    String s = "今天天气正好";
    StringUtils.isNotBlank(s);
    StringUtils.isEmpty(s);
    

    3.CollectionUtils

    //对单列集合Collection判空
    List array = new ArrayList();
    CollectionUtils.isNotEmpty(array); 
    

    4.MapUtils

    //在Map集合中根据key取value,内部都会做一个隐式判空
    Map<String,Object> map = new HashMap();
    map.put("str","字符串");
    map.put("array",new ArrayList<>());
    map.put("mapChild","new HashMap()");
    String str = MapUtils.getString(map,"str");
    List array = (List) MapUtils.getObject(map,"array");
    Map mapChild = MapUtils.getMap(map,"mapChild");
    

      

    愿你走出半生,归来仍是少年!
  • 相关阅读:
    10-3 集合之Set
    【Angular】排序
    【Mongous】
    【验证码】
    爬虫
    【DOM】
    年月日
    【Mocha】
    【Test】
    洛谷——P1823 音乐会的等待
  • 原文地址:https://www.cnblogs.com/hujunwei/p/14005005.html
Copyright © 2011-2022 走看看