工具類系列---【如何優雅的對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