logo
当前位置:首 页 > 编程技术 >后端开发 >Java > 查看文章

java 判断对象是否为空

Java, 后端开发, 编程技术 你是第2033个围观者 0条评论 供稿者: 标签:

使用Java开发Android后发现有许多地方需要判空都是需要的。之前我判空会用到size来看看是否有值,但是有的时候还是会报错,左右找了下判空的代码

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

/**
	 * 判断对象或对象数组中每一个对象是否为空: 对象为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;
	}

 

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));
 
	}

 

 

 

 

其实直接调用第二部分的代码就可以进行判空了,之前判空是因为用了fastjson后发现有时候size不稳定了,所以找了下。

 

 

 

 

 

 

 

 

 

 

 

 

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao Studio All Rights Reserved. E-mail:cjx2328#126.com(#号改成@) 沪ICP备14052271号-3