利用java反射机制,对比两个对象的差异

package com.compare;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestCompareFields {
	
	public static void main(String[] args) {
		Student s1 = new Student("1", "test1", 13214, "shenzhen");
		Student s2 = new Student("2", "test2", 123, "珠海");
		//比较S1 和 S2 的属性值,其中id忽略比较
		Map<String, List<Object>> compareResult = compareFields(s1, s2, new String[] {"id"});
		Set<String> keySet = compareResult.keySet();
		for (String key : keySet) {
			List<Object> list = compareResult.get(key);
			System.out.println(">>>  s1的" + key + "为" + list.get(0) + ",s2的" + key + "为" + list.get(1));
		}
	}
	
	/**
	 * @param obj1 进行属性比较的对象1
	 * @param obj2 进行属性比较的对象2
	 * @param ignoreArr 选择忽略比较的属性数组
	 * @return 属性差异比较结果 map 
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, List<Object>> compareFields(Object obj1,Object obj2,String[] ignoreArr){
		try {
			Map<String, List<Object>> map = new HashMap<String, List<Object>>();
			List<String> ignoreList = null;
			if (ignoreArr !=null && ignoreArr.length>0) {
				ignoreList = Arrays.asList(ignoreArr);
			}
			if (obj1.getClass() == obj2.getClass()) { //只有两个对象都是同一类型才有可比性
				Class clazz = obj1.getClass();
				//获取object的属性描述
				PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
				for (PropertyDescriptor pd : pds) {
					String name = pd.getName();
					if (ignoreList !=null && ignoreList.contains(name)) {
						continue;
					}
					Method readMethod = pd.getReadMethod();//获取属性的get方法
					//在obj1上调用get方法等同于获得obj1的属性值
					Object o1 = readMethod.invoke(obj1);
					//在obj2上调用get方法等同于获得obj2的属性值
					Object o2 = readMethod.invoke(obj2);
					//如果此属性是时间
					if (o1 instanceof Timestamp) {
						o1 = new Date(((Timestamp)o1).getTime());
					}
					if (o2 instanceof Timestamp) {
						o2 = new Date(((Timestamp)o2).getTime());
					}
					if (o1 == null && o2 == null) {
						continue;
					}else if (o1 == null && o2 !=null) {
						//不一致
						List<Object> list = new ArrayList<Object>();
						list.add(o1);
						list.add(o2);
						map.put(name, list);
						continue;
					}
					if (!o1.equals(o2)) {
						List<Object> list = new ArrayList<>();
						list.add(o1);
						list.add(o2);
						map.put(name, list);
					}
				}
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
package com.compare;

public class Student {
	private String id;
	private String name;
	private int age;
	private String city;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public Student(String id, String name, int age, String city) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.city = city;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值