Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Java判断不为空的工具类总结

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

作者头像
别先生
发布于 2018-08-09 08:11:48
发布于 2018-08-09 08:11:48
5.1K00
代码可运行
举报
文章被收录于专栏:别先生别先生
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  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 }

待续......

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
SpringBoot内置工具类,告别瞎写工具类了
        不知大家有没有注意到,接手的项目中存在多个重复的工具类,发现其中很多功能,Spring 自带的都有。于是整理了本文,希望能够帮助到大家!
军军不吃鸡
2022/10/26
8670
别瞎写工具类了,Spring自带的不香吗?
今天这篇文章专门跟大家一起总结一下,Spring框架本身自带的一些好用的工具类,希望对你会有所帮助。
苏三说技术
2024/02/06
3241
别瞎写工具类了,Spring自带的不香吗?
求你别自己瞎写工具类了,Spring自带的这些他不香麽?
最近发现同事写了不少重复的工具类,发现其中很多功能,Spring 自带的都有。于是整理了本文,希望能够帮助到大家!
Java技术江湖
2022/02/17
2K0
求你别自己瞎写工具类了,Spring自带的这些他不香麽?
Java常用工具类
转载自 http://blog.csdn.net/sinat_29581293/article/details/51969604
allsmallpig
2021/02/12
7480
Java常用工具类
12:集合map、工具类
一、map集合 Map:一次添加一对元素。Collection 一次添加一个元素。 Map也称为双列集合,Collection集合称为单列集合。 其实map集合中存储的就是键值对(结婚证书), map
六月的雨
2018/05/14
9910
Java第四周总结
开发中会使用大量相同数据类型的情况。如果使用数组来解决问题 1. 数组能够使用的方法非常少,功能方法需要程序员自己完成。 2. 数据类型单一化,不支持多种情况。 3. 数组容量不可以更改。
用户7073689
2020/03/18
8310
Java集合类
集合类其实就是为了更好地组织、管理和操作我们的数据而存在的,包括列表、集合、队列、映射等数据结构。
用户9645905
2023/10/23
2600
Java集合类
调试工具类
import java.io.*; import java.util.*; import java.text.*; import java.lang.reflect.*;
源哥
2018/08/28
1.5K0
系统学习javaweb-01-java基础语法
注意: Object类型的数组可以存储任意类型的数据 String[] arr = new String[1000];
csxiaoyao
2019/02/18
9780
Java工具集-判断(AssertUtil)
简单工具类 写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK
cwl_java
2019/10/26
1.7K0
【小家Spring】Spring Framework提供的实用纯Java工具类大合集(一)
在Spring Framework里的spring-core核心包里面,有个org.springframework.util里面有不少非常实用的工具类。
YourBatman
2019/09/03
4.8K1
【小家Spring】Spring Framework提供的实用纯Java工具类大合集(一)
Java 对字符串操作的工具类,很全
目录 1 实现 1 实现 import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.util.AntPathMatcher; import com.ruoy
一写代码就开心
2023/02/01
1.1K0
java 常用工具类 (值得收藏)
package org.fh.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStr
FHAdmin
2022/03/14
3280
字符串、集合如何判断空值?看看成年人的正确操作
在平时的开发中,基本上都会用到字符串判断空值和集合判断空值的处理,还记得在刚干开发的时候,写的代码在现在看起来是真的有点Hello World,那么这次分享两个非常常用的方法,字符串非空判断和集合非空判断。
一个程序员的成长
2020/11/25
1.4K0
Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明
如下是 Set 中的 Key 存储自定义类 Person5 ,其中并没有重写Object 中的 equals() 方法和 hashCode()方法。会出现 Key 存储到重复的数据。
RainbowSea
2023/02/03
1K0
【小家Spring】Spring Framework提供的实用纯Java工具类大合集(二)
接着上一篇 【小家Spring】Spring Framework提供的实用纯Java工具类大合集(一)
YourBatman
2019/09/03
1.3K0
【小家Spring】Spring Framework提供的实用纯Java工具类大合集(二)
Java字符串工具类,对StringUtils重写,方便使用
String工具 主要对 StringUtils 的一些方法进行重写,达到更方便的使用
目的地-Destination
2023/03/06
6990
java 常用工具类
package org.fh.util; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 说明:常用工具 * 作者:FH Admin * from:fhadmin.cn */ public class Tools { /** * 随机生成六位数验证码 * @return */ public static int getRandomNu
FHAdmin
2021/09/22
3910
Java常用工具类集合
或使用 com.ailk.org.apache.commons.lang3.StringUtils 工具类
Kevin_Zhang
2021/06/29
7160
带你认识Hutool工具包
Hutool 是一个小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以 “甜甜的”。
程序员Leo
2023/08/07
7740
带你认识Hutool工具包
相关推荐
SpringBoot内置工具类,告别瞎写工具类了
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验