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

相关文章

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在