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" ...
随机推荐
- VS code 配置C++编译环境
主要参考链接:https://blog.csdn.net/bat67/article/details/76095813 另外有如下几处需要注意的地方: (1) 这部需要提前“run build tas ...
- uniGUI -- Web 应用框架 Delphi + Ext JS
uniGUI -- Web 应用框架 ,基于 Delphi + Ext JS 技术.
- html单选按钮用jQuery中prop()方法设置
模拟单选按钮时用jQuery,prop方法来设置. 赋默认选中值:$("#" + id).find("input:radio[value='" + state ...
- 防火墙iptables的简单使用
规则定义 # service iptables start # chkconfig iptables on 想让规则生效,则shell命令行下执行 sh /bin/iptables.sh即可 [roo ...
- lanmp安装
下载安装(ssh登录服务器,执行如下操作即可,需要用到root用户权限来安装)源码编译安装wget http://dl.wdlinux.cn:5180/lanmp_laster.tar.gztar z ...
- STM32L476应用开发之四:触摸屏驱动与数据交互
数据交互可以说是任何一台仪器都需要的功能.我们的便携式气体分析仪,需要人来操作和配置,所以触摸屏就是我们必然的一个选择.本次我们计划采用3.5寸显示屏,串口通讯. 1.硬件设计 前面我们实验了串行通讯 ...
- Modbus库开发笔记之十:利用协议栈开发Mosbus RTU Slave应用
上一节我们使用协议占开发了一个Modbus TCP Server应用.接下来我们使用协议栈在开发一个基于串行链路的Mosbus RTU Slave应用. 根据前面对协议栈的封装,我们需要引用Modbu ...
- java 中int与integer的区别
int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...
- ORA-00257 archiver error. 错误的处理方法
archive log 日志已满 方法/步骤 1 SecureCRT登录服务器,切换用户oracle,连接oracle [root@userbeta~]# su - oracle [oracle@us ...
- Confluence 6 编辑站点欢迎消息使用模板编辑器的小提示
站点欢迎消息是一个模板而不是一个页面,所以你需要使用模板编辑器来对你的消息进行编辑. 你可以和在你 Confluence 中其他页面中一样,在站点欢迎消息模板中添加文本,连接和宏.但是添加图片的话会有 ...