Java判断不为空的工具类总结
1、Java判断是否为空的工具类,可以直接使用。包含,String字符串,数组,集合等等。
package com.bie.util; import java.util.Collection;
import java.util.Iterator;
import java.util.Map; /**
*
* @author biehl
*
* @date 2018年7月31日下午2:40:40
*
* @Notes 判断是否为空的工具栏,如果不使用StringUtils的jdk的工具类,可以自行封装
*
*/
public class ObjectUtils { /**
* 判断字符串不为空
* @param str
* @return
*/
public static boolean notEmpty(String str){
//StringUtils.isNotEmpty(str);
return str != null && !"".equals(str);
} /**
* 判断字符串不为空
* jdk StringUtils工具类实现如下所示
* @param str
* @return
*/
public static boolean isNotEmpty(String str){
return !isEmpty(str);
} /**
* 判断字符串为空
* @param str
* @return
*/
public static boolean isEmpty(String str){
return str == null || str.length() == ;
} /**
* 集合判断是否为空
* @param collection 使用泛型
* @return
*/
public static <T> boolean notEmpty(Collection<T> collection){
if(collection != null){
Iterator<T> iterator = collection.iterator();
if(iterator != null){
while(iterator.hasNext()){
Object next = iterator.next();
if(next != null){
return true;
}
}
}
}
return false;
} /**
* map集合不为空的判断
* @param map 使用泛型,可以传递不同的类型参数
* @return
*/
public static <T> boolean notEmpty(Map<T, T> map){
return map != null && !map.isEmpty();
} /**
* byte类型数组判断不为空
* @param t
* @return
*/
public static boolean notEmpty(byte[] t){
return t != null && t.length > ;
} /**
* short类型数组不为空判断
* @param t
* @return
*/
public static boolean notEmpty(short[] t){
return t != null && t.length > ;
} /**
* 数组判断不为空,没有泛型数组,所以还是分开写吧
* @param t 可以是int,short,byte,String,Object,long
* @return
*/
public static boolean notEmpty(int[] t){
return t != null && t.length > ;
} /**
* long类型数组不为空
* @param t
* @return
*/
public static boolean notEmpty(long[] t){
return t != null && t.length > ;
} /**
* String类型的数组不为空
* @param t
* @return
*/
public static boolean notEmpty(String[] t){
return t != null && t.length > ;
} /**
* Object类型数组不为空
* @param t
* @return
*/
public static boolean notEmpty(Object[] t){
return t != null && t.length > ;
} /**
*
* @param o
* @return
*/
public static boolean notEmpty(Object o){
return o != null && !"".equals(o) && !"null".equals(o);
} public static void main(String[] args) {
//String str = "";
//1、判断字符串是否为空notEmpty()方法
/*if(ObjectUtils.notEmpty(str)){
System.out.println("字符串 " + str + " 不为空......");
}else{
System.out.println("字符串 " + str + "为空......");
}*/ //2、判断字符串是否为空isNotEmpty()方法
/*if(ObjectUtils.isNotEmpty(str)){
System.out.println("字符串 " + str + " 不为空......");
}else{
System.out.println("字符串 " + str + "为空......");
}*/ //3、集合判断是否为空,list和set实现Collection
/*List<String> list = new ArrayList<String>();
//list.add("hello");
if(ObjectUtils.notEmpty(list)){
System.out.println("List集合不为空");
}else{
System.out.println("List集合为空");
}*/ /*Set<String> set = new HashSet<String>();
set.add("hello");
if(ObjectUtils.notEmpty(set)){
System.out.println("set集合不为空");
}else{
System.out.println("set集合为空");
}*/ //4、map集合接口,需要写单独的判读是否为空的方法
/*Map<String, String> map = new HashMap<String, String>();
//map.put("hello", "hello world");
if(ObjectUtils.notEmpty(map)){
System.out.println("map集合不为空");
}else{
System.out.println("map集合为空");
}*/ //5、数组判断不为空
/*int[] a = new int[]{1,2,3,4,5};
if(ObjectUtils.notEmpty(a)){
System.out.println("int类型数组不为空");
}else{
System.out.println("int类型数组为空");
}*/ /*byte[] b = new byte[]{1,2,3,4,5};
if(ObjectUtils.notEmpty(b)){
System.out.println("byte类型数组不为空");
}else{
System.out.println("byte类型数组为空");
} short[] c = new short[]{1,2,3,4,5};
if(ObjectUtils.notEmpty(c)){
System.out.println("short类型数组不为空");
}else{
System.out.println("short类型数组为空");
} long[] d = new long[]{1,2,3,4,5};
if(ObjectUtils.notEmpty(d)){
System.out.println("long类型数组不为空");
}else{
System.out.println("long类型数组为空");
} String[] e = new String[]{"hello","world","lisi","zhangsan"};
if(ObjectUtils.notEmpty(e)){
System.out.println("String类型数组不为空");
}else{
System.out.println("String类型数组为空");
} Object[] a = new Object[]{1,2,3,4,5};
if(ObjectUtils.notEmpty(a)){
System.out.println("Object类型数组不为空");
}else{
System.out.println("Object类型数组为空");
}*/ } }
待续......
Java判断不为空的工具类总结的更多相关文章
- java里poi操作excel的工具类(兼容各版本)
转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...
- java.util.Arrays----操作数组的工具类
java.util.Arrays操作数组的工具类,里面定义了很多操作数组的方法 1.boolean equals(int[] a,int[] b):判断两个数组是否相等. 2.String toStr ...
- Java字符串转16 进制工具类Hex.java
Java字符串转16 进制工具类Hex.java 学习了:https://blog.csdn.net/jia635/article/details/56678086 package com.strin ...
- Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
- java中redis的分布式锁工具类
使用方式 try { if(PublicLock.getLock(lockKey)){ //这里写代码逻辑,执行完后需要释放锁 PublicLock.freeLock(lockKey); } } ca ...
- java代码(12) ---CollectionUtils工具类
CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...
- 黑马程序员——【Java基础】——泛型、Utilities工具类、其他对象API
---------- android培训.java培训.期待与您交流! ---------- 一.泛型 (一)泛型概述 1.泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制 ...
- Java基础---泛型、集合框架工具类:collections和Arrays
第一讲 泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...
- Java导出防止小数显示不全工具类
1.说明 在做项目的过程中,发现导出功能中的数据显示不全,如"0.4",会显示成".4":"-0.8"会显示成"-.8" ...
随机推荐
- Trickbot展示新技巧:密码抓取器模块
Trickbot是一个简单的银行木马 来源 https://blog.trendmicro.com/trendlabs-security-intelligence/trickbot-adds-remo ...
- GAN_李弘毅讲解
GAN_李弘毅讲解: 上式中,xi从data中sample的一部分,现在的目的就是最大化这个似然函数,使得Generator最可能产生data中的这些sample: 上式中之所以如此设计V函数,是为了 ...
- Node.js的异步IO和事件轮询
想象一下,以前我们在写程序时, 如果程序在I/O上阻塞了,当有更多请求过来时,服务器会怎么处理呢?在这种情景中通常会用多线程的方式.一种常见的实现是给每个连接分配一个线程,并为那些连接设置一个线程池 ...
- Nest + typeorm
1\ Nest (https://nestjs.com/) is a framework for building efficient, scalable Node.js web appli ...
- Linux将rm命令设置为回收站【转】
一个方案就是重定向 rm 命令以嫁接为 mv 命令,相当于给 Linux 系统定制了一个回收站. 实现方式如下: ### 重定义rm命令 ### # 定义回收站目录 trash_path='~/.tr ...
- 美团点评基于MGR的CMDB高可用架构搭建之路【转】
王志朋 美团点评DBA 曾在京东金融担任DBA,目前就职于美团点评,主要负责金融业务线数据库及基础组件数据库的运维. MySQL Group Replication(以下简称MGR),于5.7.17版 ...
- HTTP协议02-请求和响应的报文构成
HTTP协议和TCP/IP协议族内的其他众多协议相同,用于客户端与服务器之间的通信,请求访问文本或图像等资源的一端+称为客户端,而提供资源响应的一端称为服务端. 应用HTTP协议时,请求必定是客户端发 ...
- windows系统yolov3的安装教程(图文)
记于 2018-05-19 13:21:13 距离开始着手安装yolov3已经过去将近20个小时,当然我并没有装那么久啦,就是大概4,5个小时这么久,网络上教程很少,步骤也千奇百怪,这次成功装好后就想 ...
- git pull/fectch
git remote: show all remote repositories git push -u <X1> <>: set x1 as the default repo ...
- 【原创】测试基础之http_load(1)简介、安装、使用
http_load-09Mar2016官方:https://acme.com/software/http_load/ 一 简介 http_load - multiprocessing http tes ...