下面是一個通用的方法,判斷字元串是否為空,集合是否為空,數組是否為空:
/**
* 判斷對象或對象數組中每一個對象是否為空: 對象為null,字元序列長度為0,集合類、map為empty
*
* @param obj
* @return
*/
public static boolean isnullorempty(object obj) {
if (obj == null)
return true;
if (obj instanceof charsequence)
return ((charsequence) obj).length() == 0;
if (obj instanceof collection)
return ((collection) obj).isempty();
if (obj instanceof map)
return ((map) obj).isempty();
if (obj instanceof object[]) {
object[] object = (object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i < object.length; i++) {
if (!isnullorempty(object[i])) {
empty = false;
break;
}
return empty;
}
return false;
}
參考:org.apache.commons.lang.stringutils
應用場景:
讀取excel檔案,轉化為一個二維數組:object[][] arrays
但是excel中有空行,是以需要過濾object[][] arrays中的空的一維數組:
/***
* 過濾數組中的空元素
* @param arrays
public static object[][] filterempty(object[][] arrays) {
int sumnotnull = 0;
/***
* 統計非空元素的總個數
*/
for (int i = 0; i < arrays.length; i++) {
object object = arrays[i];
if (!valuewidget.isnullorempty(object)
&& !systemutil.isnullorempty((object[]) object)) {// 判斷元素是否為空
sumnotnull = sumnotnull + 1;
object[][] filtedobjs = new object[sumnotnull][];
int index = 0;
object[] object_tmp = arrays[i];
if (!valuewidget.isnullorempty(object_tmp)
&& !systemutil.isnullorempty((object[]) object_tmp)) {// 判斷元素是否為空
filtedobjs[index++] = object_tmp;
return filtedobjs;
參閱附件中的方法com.common.util.systemutil.filterempty
判斷對象的所有成員變量是否為空
* determine whether the object's fields are empty
* @param isexcludezero :true:數值類型的值為0,則當做為空;----false:數值類型的值為0,則不為空
* @throws securityexception
* @throws illegalargumentexception
* @throws nosuchfieldexception
* @throws illegalaccessexception
public static boolean isnullobject(object obj, boolean isexcludezero)
throws securityexception, illegalargumentexception,
nosuchfieldexception, illegalaccessexception {
if(valuewidget.isnullorempty(obj)){//對象本身就為null
list<field> fieldlist = reflecthwutils.getallfieldlist(obj.getclass());
boolean isnull = true;
for (int i = 0; i < fieldlist.size(); i++) {
field f = fieldlist.get(i);
object propertyvalue = null;
try {
propertyvalue = getobjectvalue(obj, f);
} catch (nosuchfieldexception e) {
e.printstacktrace();
if (!valuewidget.isnullorempty(propertyvalue)) {// 字段不為空
if (propertyvalue instanceof integer) {// 是數字
if (!((integer) propertyvalue == 0 && isexcludezero)) {
isnull = false;
break;
}
} else if (propertyvalue instanceof double) {// 是數字
if (!((double) propertyvalue == 0 && isexcludezero)) {
}else if (propertyvalue instanceof float) {// 是數字
if (!((float) propertyvalue == 0 && isexcludezero)) {
}else if (propertyvalue instanceof short) {// 是數字
if (!((short) propertyvalue == 0 && isexcludezero)) {
}else {
isnull = false;
return isnull;
測試:
@test
public void test_isnullobject() throws securityexception,
illegalargumentexception, nosuchfieldexception,
illegalaccessexception {
person2 p = new person2();
assert.assertequals(true, reflecthwutils.isnullobject(p, true));
assert.assertequals(false, reflecthwutils.isnullobject(p, false));
p.setaddress("beijing");
assert.assertequals(false, reflecthwutils.isnullobject(p, true));
p.setaddress(null);
p.setid(0);
person2 源代碼(省略getter,setter方法):
import java.sql.timestamp;
public class person2 {
private int id;
private int age;
private double weight;
private string personname;
private timestamp birthdate;
public string identitify;
protected string address;
string phone;
}