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" ...
随机推荐
- Ubuntu的内核转储工具【转】
转自:http://www.cnblogs.com/wwang/archive/2010/11/19/1881304.html 在我的上一篇博文<Linux内核的Oops>的最后介绍到一个 ...
- delphi 的 ORM 框架
delphi 的 ORM 框架,收集如下: 1.EntityDac https://www.devart.com/entitydac/download.html 2.mORMot The ...
- motor helper
# -*- coding: utf-8 -*- # @Time : 2019-02-13 10:44 # @Author : cxa # @File : mongohelper.py # @Softw ...
- Git学习笔记01-安装Git
学习的资料来自廖雪峰官方网站的Git教程,菜鸟教程的Git教程 Git是一个分布式版本控制工具,在windows上使用Git可以直接从官网下载,然后默认选项安装即可. 安装完成后,在开始菜单中找到找到 ...
- 解决64bit不能连接access的问题
原有的程序迁移至64位,结果调用数据库时出错,原因是jet驱动没有64位的,得换用64位的驱动程序: 1. 下载运行 AccessDatabaseEngine_x64.exe (http://ww ...
- 消息队列:JMS之基本概念介绍
摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...
- 【BZOJ 2119】股市的预测(SAM)
SAM很好用的啊... 传送门 双倍经验:L-Gap Substrings 基本做法类似,这道题的差分改掉,map 改掉就好了QWQ noteskey 反正就是先差分一下,然后把首项丢掉(没有比较的对 ...
- [JLOI2011]飞行路线 不同的算法,不同的悲伤
题目 :BZOJ2763 洛谷P4568 [JLOI2011]飞行路线 一道最短路的题目,想想写个题解也不错(好久没写题解了_(:з」∠)_) 然后这道题中心思路是dijikstra处理最短路,所以没 ...
- 通过python统计nginx日志定位php网站响应慢的问题
# 公司网站反映很慢,可能是一些页面的访问方法或者页面引起,通过程序统计nginx访问日志的页面和具体的action方法访问次数以及平均响应时间可以为程序开发的同事提供参考定位具体的代码 # 默认的n ...
- mysql:赋予用户权限、查看及修改端口号
一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...