工具类系列---【如何优雅的对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");

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM