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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La