java 判断不为空_Java判断不为空的工具类总结
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() == 0;
}
/**
* 集合判断是否为空
* @param collection 使用泛型
* @return
*/
public static boolean notEmpty(Collection collection){
if(collection != null){
Iterator 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 boolean notEmpty(Map map){
return map != null && !map.isEmpty();
}
/**
* byte类型数组判断不为空
* @param t
* @return
*/
public static boolean notEmpty(byte[] t){
return t != null && t.length > 0;
}
/**
* short类型数组不为空判断
* @param t
* @return
*/
public static boolean notEmpty(short[] t){
return t != null && t.length > 0;
}
/**
* 数组判断不为空,没有泛型数组,所以还是分开写吧
* @param t 可以是int,short,byte,String,Object,long
* @return
*/
public static boolean notEmpty(int[] t){
return t != null && t.length > 0;
}
/**
* long类型数组不为空
* @param t
* @return
*/
public static boolean notEmpty(long[] t){
return t != null && t.length > 0;
}
/**
* String类型的数组不为空
* @param t
* @return
*/
public static boolean notEmpty(String[] t){
return t != null && t.length > 0;
}
/**
* Object类型数组不为空
* @param t
* @return
*/
public static boolean notEmpty(Object[] t){
return t != null && t.length > 0;
}
/**
*
* @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 list = new ArrayList();
//list.add("hello");
if(ObjectUtils.notEmpty(list)){
System.out.println("List集合不为空");
}else{
System.out.println("List集合为空");
}*/
/*Set set = new HashSet();
set.add("hello");
if(ObjectUtils.notEmpty(set)){
System.out.println("set集合不为空");
}else{
System.out.println("set集合为空");
}*/
//4、map集合接口,需要写单独的判读是否为空的方法
/*Map map = new HashMap();
//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判断不为空的工具类总结相关推荐
- java中集合类的转换_Java中的两个常用工具类及集合数组的相互转换
为了编程人员的方便及处理数据的安全性,Java特别提供了两个非常有用的工具类: 一.Collections 1.Collections类的特点: 集合框架的工具类.里面定义的都是静态方法. 2.Col ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: 1 import java.text.SimpleDateFormat; 2 import java.util.Cale ...
- Java利用反射封装DBUtil,mysql万能增删改查工具类,附源码
Java利用反射封装DBUtil,mysql万能增删改查工具类,附源码 等有时间再慢慢写代码注释吧,先把源码放出来.文章最后有整个项目的压缩包. ps:拓展 Java 原生MySQL JDBC 插入后 ...
- java 判断对象的属性是否为空_Java 判断实体对象及所有属性是否为空的操作
1.判断实体对象是否为空 2.判断对象所有属性是否为空 3.特别注意,实体类中如果有基本数据类型,会影响判断 package com.liuxd.object; import org.apache.c ...
- java怎么判断json是否为空_java判断jsonObject和jsonArray是否为空
resJsonObj = {"res":"0","msg":"","data":{"Nam ...
- java if判断是否为空_java 判断对象是否为空
java 中如何判断一个未知对象是否为空呢? 下面是一个通用的方法,判断字符串是否为空,集合是否为空,数组是否为空: /** * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度 ...
- java怎么判断对象不为空_java判断对象是否为空的方法
java判断对象是否为空的方法 发布时间:2020-06-25 14:39:17 来源:亿速云 阅读:134 作者:Leah 这篇文章将为大家详细讲解有关java判断对象是否为空的方法,文章内容质量较 ...
- java判断对象属性为空_Java判断对象属性全为空
在项目中,处理复杂页面提交的时候,比如批量添加文章的时候,下面这种情况 当前页面存在多篇文章对象:后台我们接收一般使用 List,如下面这种格式提交news[0].content news[0].ti ...
- java判断时间是不是星期五_Java判断当前日期是星期几
参考链接地址:http://blog.csdn.net/a9529lty/article/details/3206942 /** * 判断当前日期是星期几 * @param pTime 修要判断的时间 ...
最新文章
- 【Linux】Linux简单操作之安装jdk
- 面经 cisco 2
- 大佬教你Android如何实现时间线效果
- 艾伟_转载:C#中的委托和事件-抛砖引玉
- C# 11 中的参数 null 检查
- LeetCode 1010. 总持续时间可被 60 整除的歌曲(哈希)
- jquery获取radio值
- sql中截取字符串函数_SQL Server 2017中的顶级SQL字符串函数
- php获取时间计算时间差
- 美国目前最流行的五种量化交易模型
- gif透明背景动画_Gifox for Mac(Gif动画录制工具)
- PreparedStatement 大数据查询
- 程序员之间的幽默段子,句句都是经典
- 强行调用html5,Html5踩坑记之mandMobile使用小记
- rabbitMQ无法访问web管理页面
- webView.addJavascriptInterface 用法
- MAC快速查看本地 SSH KEY
- python定义公民类、变量有身份证号_Python趣解身份证号码的奥秘!
- Chromium硬件加速渲染的OpenGL命令执行过程分析
- 亚马逊测评自养号,小白应该如何开始?