Java判断不为空的工具类总结

2024-06-14 13:58
文章标签 java 工具 总结 判断 不为

本文主要是介绍Java判断不为空的工具类总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Java判断不为空的工具类总结

1、Java判断是否为空的工具类,可以直接使用。包含,String字符串,数组,集合等等。

  1 package com.bie.util;
  2 
  3 import java.util.Collection;
  4 import java.util.Iterator;
  5 import java.util.Map;
  6 
  7 /**
  8  * 
  9  * @author biehl
 10  *
 11  * @date 2018年7月31日下午2:40:40
 12  * 
 13  * @Notes 判断是否为空的工具栏,如果不使用StringUtils的jdk的工具类,可以自行封装
 14  *
 15  */
 16 public class ObjectUtils {
 17 
 18     
 19     /**
 20      * 判断字符串不为空
 21      * @param str
 22      * @return
 23      */
 24     public static boolean notEmpty(String str){
 25         //StringUtils.isNotEmpty(str);
 26         return str != null && !"".equals(str);
 27     }
 28     
 29     /**
 30      * 判断字符串不为空
 31      * jdk StringUtils工具类实现如下所示
 32      * @param str
 33      * @return
 34      */
 35     public static boolean isNotEmpty(String str){
 36         return !isEmpty(str);
 37     }
 38     
 39     /**
 40      * 判断字符串为空
 41      * @param str
 42      * @return
 43      */
 44     public static boolean isEmpty(String str){
 45         return str == null || str.length() == 0;
 46     }
 47     
 48     /**
 49      * 集合判断是否为空
 50      * @param collection 使用泛型
 51      * @return
 52      */
 53     public static <T> boolean notEmpty(Collection<T> collection){
 54         if(collection != null){
 55             Iterator<T> iterator = collection.iterator();
 56             if(iterator != null){
 57                 while(iterator.hasNext()){
 58                     Object next = iterator.next();
 59                     if(next != null){
 60                         return true;
 61                     }
 62                 }
 63             }
 64         }
 65         return false;
 66     }
 67     
 68     /**
 69      * map集合不为空的判断
 70      * @param map 使用泛型,可以传递不同的类型参数
 71      * @return
 72      */
 73     public static <T> boolean notEmpty(Map<T, T> map){
 74         return map != null && !map.isEmpty(); 
 75     }
 76     
 77     /**
 78      * byte类型数组判断不为空
 79      * @param t
 80      * @return
 81      */
 82     public static boolean notEmpty(byte[] t){
 83         return t != null && t.length > 0;
 84     }
 85     
 86     /**
 87      * short类型数组不为空判断
 88      * @param t
 89      * @return
 90      */
 91     public static boolean notEmpty(short[] t){
 92         return t != null && t.length > 0;
 93     }
 94     
 95     /**
 96      * 数组判断不为空,没有泛型数组,所以还是分开写吧
 97      * @param t 可以是int,short,byte,String,Object,long
 98      * @return 
 99      */
100     public static boolean notEmpty(int[] t){
101         return t != null && t.length > 0;
102     }
103     
104     /**
105      * long类型数组不为空
106      * @param t
107      * @return
108      */
109     public static boolean notEmpty(long[] t){
110         return t != null && t.length > 0;
111     }
112     
113     /**
114      * String类型的数组不为空
115      * @param t
116      * @return
117      */
118     public static boolean notEmpty(String[] t){
119         return t != null && t.length > 0;
120     }
121     
122     /**
123      * Object类型数组不为空
124      * @param t
125      * @return
126      */
127     public static boolean notEmpty(Object[] t){
128         return t != null && t.length > 0;
129     }
130     
131     /**
132      * 
133      * @param o
134      * @return
135      */
136     public static boolean notEmpty(Object o){
137         return o != null && !"".equals(o) && !"null".equals(o); 
138     }
139     
140     public static void main(String[] args) {
141         //String str = "";
142         //1、判断字符串是否为空notEmpty()方法
143         /*if(ObjectUtils.notEmpty(str)){
144             System.out.println("字符串 " + str + " 不为空......");
145         }else{
146             System.out.println("字符串 " + str + "为空......");
147         }*/
148         
149         //2、判断字符串是否为空isNotEmpty()方法
150         /*if(ObjectUtils.isNotEmpty(str)){
151             System.out.println("字符串 " + str + " 不为空......");
152         }else{
153             System.out.println("字符串 " + str + "为空......");
154         }*/
155         
156         //3、集合判断是否为空,list和set实现Collection
157         /*List<String> list = new ArrayList<String>();
158         //list.add("hello");
159         if(ObjectUtils.notEmpty(list)){
160             System.out.println("List集合不为空");
161         }else{
162             System.out.println("List集合为空");
163         }*/
164         
165         /*Set<String> set = new HashSet<String>();
166         set.add("hello");
167         if(ObjectUtils.notEmpty(set)){
168             System.out.println("set集合不为空");
169         }else{
170             System.out.println("set集合为空");
171         }*/
172         
173         //4、map集合接口,需要写单独的判读是否为空的方法
174         /*Map<String, String> map = new HashMap<String, String>();
175         //map.put("hello", "hello world");
176         if(ObjectUtils.notEmpty(map)){
177             System.out.println("map集合不为空");
178         }else{
179             System.out.println("map集合为空");
180         }*/
181         
182         //5、数组判断不为空
183         /*int[] a = new int[]{1,2,3,4,5};
184         if(ObjectUtils.notEmpty(a)){
185             System.out.println("int类型数组不为空");
186         }else{
187             System.out.println("int类型数组为空");
188         }*/
189         
190         /*byte[] b = new byte[]{1,2,3,4,5};
191         if(ObjectUtils.notEmpty(b)){
192             System.out.println("byte类型数组不为空");
193         }else{
194             System.out.println("byte类型数组为空");
195         }
196         
197         short[] c = new short[]{1,2,3,4,5};
198         if(ObjectUtils.notEmpty(c)){
199             System.out.println("short类型数组不为空");
200         }else{
201             System.out.println("short类型数组为空");
202         }
203         
204         
205         long[] d = new long[]{1,2,3,4,5};
206         if(ObjectUtils.notEmpty(d)){
207             System.out.println("long类型数组不为空");
208         }else{
209             System.out.println("long类型数组为空");
210         }
211         
212         
213         String[] e = new String[]{"hello","world","lisi","zhangsan"};
214         if(ObjectUtils.notEmpty(e)){
215             System.out.println("String类型数组不为空");
216         }else{
217             System.out.println("String类型数组为空");
218         }
219         
220         Object[] a = new Object[]{1,2,3,4,5};
221         if(ObjectUtils.notEmpty(a)){
222             System.out.println("Object类型数组不为空");
223         }else{
224             System.out.println("Object类型数组为空");
225         }*/
226         
227         
228     }
229     
230 }

 

待续......

posted @ 2018-08-02 15:26 别先生 阅读( ...) 评论( ...) 编辑 收藏

这篇关于Java判断不为空的工具类总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1060603

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置