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判断不为空的工具类总结的更多相关文章

  1. java里poi操作excel的工具类(兼容各版本)

    转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...

  2. java.util.Arrays----操作数组的工具类

    java.util.Arrays操作数组的工具类,里面定义了很多操作数组的方法 1.boolean equals(int[] a,int[] b):判断两个数组是否相等. 2.String toStr ...

  3. Java字符串转16 进制工具类Hex.java

    Java字符串转16 进制工具类Hex.java 学习了:https://blog.csdn.net/jia635/article/details/56678086 package com.strin ...

  4. Java中的AES加解密工具类:AESUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...

  5. java中redis的分布式锁工具类

    使用方式 try { if(PublicLock.getLock(lockKey)){ //这里写代码逻辑,执行完后需要释放锁 PublicLock.freeLock(lockKey); } } ca ...

  6. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  7. 黑马程序员——【Java基础】——泛型、Utilities工具类、其他对象API

    ---------- android培训.java培训.期待与您交流! ---------- 一.泛型 (一)泛型概述 1.泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制 ...

  8. Java基础---泛型、集合框架工具类:collections和Arrays

    第一讲     泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...

  9. Java导出防止小数显示不全工具类

    1.说明 在做项目的过程中,发现导出功能中的数据显示不全,如"0.4",会显示成".4":"-0.8"会显示成"-.8" ...

随机推荐

  1. PTA L1题目合集(更新至2019.3)

    L1-001 Hello World (5 分) 链接:https://pintia.cn/problem-sets/994805046380707840/problems/9948051471320 ...

  2. linux添加swap分区【转】

    概述 添加交换分区主要是因为安装oracle时碰到交换分区太小时无法安装的情况,这时候就需要添加交换分区了. 操作简介 增加swap分区方法: 1.新建磁盘分区作为swap分区 2.用文件作为swap ...

  3. python练习实例

    #!/usr/bin/python # -*- coding: UTF-8 -*- try: fh = open("testfile","w") fh.writ ...

  4. Mudo C++网络库第六章学习笔记

    muduo网络库简介 高级语言(Java, Python等)的Sockects库并没有对Sockects API提供更高层的封装, 直接用它编写程序很容易掉到陷阱中: 网络库的价值还在于能方便地处理并 ...

  5. 024_nginx之backlog坑

    一. 线上碰到一个nginx调优的一个设置,即listen后面设置 listen 80 backlog=1024; 但是多个域名都设置这个值的时候就会出现以下的提示重复报错. 关于backlog参数的 ...

  6. linux备忘簿

    1.ubuntu中按ctrl+s锁定屏幕,按ctrl+q解锁. 2.vim中撤销和恢复为u和ctlr+r 3.静态库和动态库编译命令: (1)得到hello.o g++ -c hello.cpp (2 ...

  7. LabVIEW-PC-PLC-MCU串口通信-介绍很全

    链接:https://pan.baidu.com/s/1dG9nuPN 密码:vnpf

  8. Laravel-Excel 导入 Excel 文件----为什么只获取到最后一行数据?

    ### 今天使用了Laravel-Excel 到类文件,想做一个excel  文件到导入和导出,但是看了 官方到文档示例,自己做了一下,发现 只取到到最后一行到数据, 有点摸不着头脑! 网上找了一下, ...

  9. nginx负载均衡后端tomcat无法加载js资源

    JS或css无法完全加载 nginx的代理缓存区,默认较小导致部分文件出现加载不全的问题,比较典型的如jQuery框架,可以通过配置调整nginx的缓存区即可.主要参考proxy参数 最终完整配置如下 ...

  10. PyJWT 使用

    最近要用 Falsk 开发一个大点的后端,为了安全考虑,弃用传统的Cookie验证.转用JWT. 其实 Falsk 有一个 Falsk-JWT 但是我觉得封装的太高,还是喜欢通用的 PyJWT . J ...