java 判断对象是否为空

java 中如何判断一个未知对象是否为空呢?

下面是一个通用的方法,判断字符串是否为空,集合是否为空,数组是否为空:

Java代码   收藏代码
  1. /** 
  2.      * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty 
  3.      *  
  4.      * @param obj 
  5.      * @return 
  6.      */  
  7.     public static boolean isNullOrEmpty(Object obj) {  
  8.         if (obj == null)  
  9.             return true;  
  10.   
  11.         if (obj instanceof CharSequence)  
  12.             return ((CharSequence) obj).length() == 0;  
  13.   
  14.         if (obj instanceof Collection)  
  15.             return ((Collection) obj).isEmpty();  
  16.   
  17.         if (obj instanceof Map)  
  18.             return ((Map) obj).isEmpty();  
  19.   
  20.         if (obj instanceof Object[]) {  
  21.             Object[] object = (Object[]) obj;  
  22.             if (object.length == 0) {  
  23.                 return true;  
  24.             }  
  25.             boolean empty = true;  
  26.             for (int i = 0; i < object.length; i++) {  
  27.                 if (!isNullOrEmpty(object[i])) {  
  28.                     empty = false;  
  29.                     break;  
  30.                 }  
  31.             }  
  32.             return empty;  
  33.         }  
  34.         return false;  
  35.     }  

 参考:org.apache.commons.lang.StringUtils

 

应用场景:

读取excel文件,转化为一个二维数组:Object[][] arrays

但是excel中有空行,所以需要过滤Object[][] arrays中的空的一维数组:

Java代码   收藏代码
  1. /*** 
  2.      * 过滤数组中的空元素 
  3.      *  
  4.      *  
  5.      * @param arrays 
  6.      * @return 
  7.      */  
  8.     public static Object[][] filterEmpty(Object[][] arrays) {  
  9.         int sumNotNull = 0;  
  10.         /*** 
  11.          * 统计非空元素的总个数 
  12.          */  
  13.         for (int i = 0; i < arrays.length; i++) {  
  14.             Object object = arrays[i];  
  15.             if (!ValueWidget.isNullOrEmpty(object)  
  16.                     && !SystemUtil.isNullOrEmpty((Object[]) object)) {// 判断元素是否为空  
  17.                 sumNotNull = sumNotNull + 1;  
  18.             }  
  19.         }  
  20.         Object[][] filtedObjs = new Object[sumNotNull][];  
  21.         int index = 0;  
  22.         for (int i = 0; i < arrays.length; i++) {  
  23.             Object[] object_tmp = arrays[i];  
  24.             if (!ValueWidget.isNullOrEmpty(object_tmp)  
  25.                     && !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 判断元素是否为空  
  26.                 filtedObjs[index++] = object_tmp;  
  27.             }  
  28.         }  
  29.         return filtedObjs;  
  30.     }  

 参阅附件中的方法com.common.util.SystemUtil.filterEmpty

 

判断对象的所有成员变量是否为空

Java代码   收藏代码
  1. /*** 
  2.      * Determine whether the object's fields are empty 
  3.      *  
  4.      * @param obj 
  5.      * @param isExcludeZero  :true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空 
  6.      *  
  7.      * @return 
  8.      * @throws SecurityException 
  9.      * @throws IllegalArgumentException 
  10.      * @throws NoSuchFieldException 
  11.      * @throws IllegalAccessException 
  12.      */  
  13.     public static boolean isNullObject(Object obj, boolean isExcludeZero)  
  14.             throws SecurityException, IllegalArgumentException,  
  15.             NoSuchFieldException, IllegalAccessException {  
  16.         if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null  
  17.             return true;  
  18.         }  
  19.         List<Field> fieldList = ReflectHWUtils.getAllFieldList(obj.getClass());  
  20.         boolean isNull = true;  
  21.         for (int i = 0; i < fieldList.size(); i++) {  
  22.             Field f = fieldList.get(i);  
  23.             Object propertyValue = null;  
  24.             try {  
  25.                 propertyValue = getObjectValue(obj, f);  
  26.             } catch (NoSuchFieldException e) {  
  27.                 e.printStackTrace();  
  28.             }  
  29.   
  30.             if (!ValueWidget.isNullOrEmpty(propertyValue)) {// 字段不为空  
  31.                 if (propertyValue instanceof Integer) {// 是数字  
  32.                     if (!((Integer) propertyValue == 0 && isExcludeZero)) {  
  33.                         isNull = false;  
  34.                         break;  
  35.                     }  
  36.                 } else if (propertyValue instanceof Double) {// 是数字  
  37.                     if (!((Double) propertyValue == 0 && isExcludeZero)) {  
  38.                         isNull = false;  
  39.                         break;  
  40.                     }  
  41.                 }else if (propertyValue instanceof Float) {// 是数字  
  42.                     if (!((Float) propertyValue == 0 && isExcludeZero)) {  
  43.                         isNull = false;  
  44.                         break;  
  45.                     }  
  46.                 }else if (propertyValue instanceof Short) {// 是数字  
  47.                     if (!((Short) propertyValue == 0 && isExcludeZero)) {  
  48.                         isNull = false;  
  49.                         break;  
  50.                     }  
  51.                 }else {  
  52.                     isNull = false;  
  53.                     break;  
  54.                 }  
  55.             }  
  56.         }  
  57.         return isNull;  
  58.     }  

 测试:

Java代码   收藏代码
  1. @Test  
  2.     public void test_isNullObject() throws SecurityException,  
  3.             IllegalArgumentException, NoSuchFieldException,  
  4.             IllegalAccessException {  
  5.         Person2 p = new Person2();  
  6.         Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));  
  7.         Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  
  8.   
  9.         p.setAddress("beijing");  
  10.         Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true));  
  11.         Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  
  12.   
  13.         p.setAddress(null);  
  14.         p.setId(0);  
  15.         Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));  
  16.         Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  
  17.   
  18.     }  

 Person2 源代码(省略getter,setter方法):

Java代码   收藏代码
  1. import java.sql.Timestamp;  
  2.   
  3. public class Person2 {  
  4.     private int id;  
  5.     private int age;  
  6.     private double weight;  
  7.     private String personName;  
  8.     private Timestamp birthdate;  
  9.     public String identitify;  
  10.     protected String address;  
  11.     String phone;  
  12.       
  13. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值