閱讀487 返回首頁    go 阿裏雲 go 技術社區[雲棲]


java 判斷對象是否為空

java 中如何判斷對象是否為空呢,特別是一個未知的對象?

下麵是一個通用的方法,判斷字符串是否為空,集合是否為空,數組是否為空:

/**
	 * 判斷對象或對象數組中每一個對象是否為空: 對象為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;
	}

上述方法運用了遞歸,當對象是數組時又調用自身.

測試:

@Test
	public void test_isNullOrEmpty(){
		String input1=null;
		String input3="";
		String input4=" ";
		String input5="a";
		String input6="   ";
		String[] strs1 = new String[] { "a" };
		String[] strs2 = new String[] { "", "" };
		String[] strs3 = new String[] { "", "c" };
		String[] strs4 = new String[] {};
		org.junit.Assert.assertEquals(true, ValueWidget.isNullOrEmpty(input1));
		org.junit.Assert.assertEquals(true, ValueWidget.isNullOrEmpty(input3));
		org.junit.Assert.assertEquals(false, ValueWidget.isNullOrEmpty(input4));
		org.junit.Assert.assertEquals(false, ValueWidget.isNullOrEmpty(input5));
		org.junit.Assert.assertEquals(false, ValueWidget.isNullOrEmpty(input6));
		
		org.junit.Assert.assertEquals(false, ValueWidget.isNullOrEmpty(strs1));
		org.junit.Assert.assertEquals(true, ValueWidget.isNullOrEmpty(strs2));
		org.junit.Assert.assertEquals(false, ValueWidget.isNullOrEmpty(strs3));
		org.junit.Assert.assertEquals(true, ValueWidget.isNullOrEmpty(strs4));
	}

那麼如何判斷一個自定義對象屬性是否全為空呢?(這裏隻考慮對象的屬性全為基本類型)?

代碼如下:

/***
	 * Determine whether the object's fields are empty
	 * 
	 * @param obj
	 * @param isExcludeZero  :true:數值類型的值為0,則當做為空;----false:數值類型的值為0,則不為空
	 * 
	 * @return
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public static boolean isNullObject(Object obj, boolean isExcludeZero)
			throws SecurityException, IllegalArgumentException,
			NoSuchFieldException, IllegalAccessException {
		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)) {
						isNull = false;
						break;
					}
				}else if (propertyValue instanceof Float) {// 是數字
					if (!((Float) propertyValue == 0 && isExcludeZero)) {
						isNull = false;
						break;
					}
				}else if (propertyValue instanceof Short) {// 是數字
					if (!((Short) propertyValue == 0 && isExcludeZero)) {
						isNull = false;
						break;
					}
				}else {
					isNull = false;
					break;
				}
			}
		}
		return isNull;
	}
ValueWidget.isNullOrEmpty 的類com.string.widget.util.ValueWidget.ValueWidget:見 

https://download.csdn.net/detail/hw1287789687/7255307
測試:

package com.test.bean;

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;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public Timestamp getBirthdate() {
		return birthdate;
	}
	public void setBirthdate(Timestamp birthdate) {
		this.birthdate = birthdate;
	}
	public String getIdentitify() {
		return identitify;
	}
	public void setIdentitify(String identitify) {
		this.identitify = identitify;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	
	
}

@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));
		Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));

		p.setAddress(null);
		p.setId(0);
		Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));
		Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));

	}
參考:https://hw1287789687.iteye.com/blog/1936364
項目代碼下載地址:

https://download.csdn.net/detail/hw1287789687/7255307
.





最後更新:2017-04-03 12:56:25

  上一篇:go SVN切換分支從舊版本到新版本
  下一篇:go 一些概念