java中常用的判断数据为空的工具
一些工具的引入可以是的代码变得更加整洁,同时对于数据的判断会更加的全面
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.java</groupId> <artifactId>rabbitmq</artifactId> <version> 1.0 -SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 5 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 70 </version> </dependency> <!--StringUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.4 </version> </dependency> <!--MapUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version> 4.2 </version> </dependency> <!--CollectionUtils ObjectUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.9 </version> </dependency> </dependencies> </project> |
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.java; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 17:29 */ public class Cat { private String name; private Integer age; private String color; public Cat() { } public Cat(String name, Integer age, String color) { this .name = name; this .age = age; this .color = color; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.java; import org.apache.commons.lang3.ObjectUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 17:44 */ public class ReflectUtil { private static final Logger logger= LoggerFactory.getLogger(ReflectUtil. class ); /** * 判断获取的实体类对象是否为空 * @param obj * @return */ public static boolean isEmpty(Object obj) { boolean flag= true ; if (ObjectUtils.isNotEmpty(obj)) { Class<?> aClass = obj.getClass(); Field[] fields = aClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible( true ); Object fieldValue = null ; try { fieldValue = field.get(obj); } catch (IllegalAccessException e) { logger.error( "获取{},异常:{}" ,fieldValue,e); } if (ObjectUtils.isNotEmpty(fieldValue)){ flag= false ; } } return flag; } else { return flag; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.java; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 16:19 */ public class UtilsTests { private final Logger logger= LoggerFactory.getLogger(UtilsTests. class ); @Test public void test(){ String string= "123" ; String string1= null ; String string2= "" ; logger.info( "" +StringUtils.isEmpty(string)); logger.info( "" +StringUtils.isEmpty(string1)); logger.info( "" +StringUtils.isEmpty(string2)); HashMap<String,Object> map= null ; HashMap<String,Object> map1= new HashMap<>( 16 ); HashMap<String,Object> map2= new HashMap<>( 16 ); map2.put( "aa" , "1111" ); logger.info( "MapUtils " + MapUtils.isEmpty(map)); logger.info( "MapUtils " + MapUtils.isEmpty(map1)); logger.info( "MapUtils " + MapUtils.isEmpty(map2)); List<String> list= null ; List<String> list1= new ArrayList<>( 10 ); List<String> list2= new ArrayList<>( 10 ); list2.add( "111" ); logger.info( "CollectionUtils " + CollectionUtils.isEmpty(list)); logger.info( "CollectionUtils " + CollectionUtils.isEmpty(list1)); logger.info( "CollectionUtils " + CollectionUtils.isEmpty(list2)); Cat cat= new Cat(); Cat cat1= new Cat( "兔兔" , 1 , "黑色" ); Cat cat2= null ; logger.info( "实体类对象判断 " + ReflectUtil.isEmpty(cat)); logger.info( "实体类对象判断 " + ReflectUtil.isEmpty(cat1)); logger.info( "实体类对象判断 " + ReflectUtil.isEmpty(cat2)); } } |
效果图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异