java判断集合是否被修改_【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更...

packagecom.sxd.streamTest;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.serializer.SerializerFeature;importjava.lang.reflect.Field;import java.util.*;/*** @ClassName Test

* @Description 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更

* @Author sxd

* @Date 2020/02/14 10:07

* @Version 1.0*/

public classTest {//忽略的属性

List ignoreFields = Arrays.asList("yn","id");/*** 测试的入口方法,实例化几个对象*/@org.junit.Testpublic voidtest(){

Date modifyTime= newDate();

Date modifyTime2= newDate();

List list1 = new ArrayList<>();

list1.add(1L);

list1.add(2L);

List list2 = new ArrayList<>();

list1.add(1L);

Student student1= newStudent();

student1.setAge(1);

student1.setName("张三1");

student1.setModifyTime(modifyTime);

student1.setStoreIdList(list1);

student1.setYn(1);

Student student2= newStudent();

student2.setAge(1);

student2.setName("张三1");

student2.setModifyTime(modifyTime2);

student2.setStoreIdList(list2);

student2.setYn(-1);

Student student3= newStudent();

student3.setAge(3);

student3.setName("张三3");

student3.setModifyTime(modifyTime);

student3.setYn(1);

Student student4= newStudent();

student4.setAge(4);

student4.setName("张三4");

student4.setModifyTime(modifyTime2);

student4.setYn(-1);

Student student5= newStudent();

student5.setAge(4);

student5.setName("张三4");

student5.setModifyTime(modifyTime2);

student5.setYn(11);

List sList1 =Arrays.asList(student2,student1);

List sList2 =Arrays.asList(student4,student3);

System.out.println("List集合是否变更:" +listModifyContrast(sList2,sList1));

System.out.println("单个对象是否变更:" + objModifyContrast(student4,student5,false));

}/*** List数据变更对比

* 比较两个相同泛型的List集合中值是否相同

*@paramnewList

*@paramoldList

*@returntrue有变更 false无变更*/

public Boolean listModifyContrast(List extends Object> newList, List extends Object>oldList){boolean flag = false;if (newList != null && oldList != null){if (newList.size() !=oldList.size()){

flag= true;

}else{int same = 0;for(Object newObj : newList) {for(Object oldObj : oldList) {if(isBaseType(newObj)){if (newObj ==oldObj){

same++;

}

}else{if (!objModifyContrast(newObj,oldObj,false)){

same++;

}

}

}

}if (same !=newList.size()){

flag= true;

}

}

}else{if (newList == null && oldList == null){

flag= false;

}else{

flag= true;

}

}returnflag;

}/*** 判断obj是否基本数据类型

* 这里Integer和int 都认定为基本类型

*@paramobj

*@return

*/

private booleanisBaseType(Object obj){

Class aClass=obj.getClass();if (aClass == Integer.class || aClass ==Integer.TYPE|| aClass == Byte.class || aClass ==Byte.TYPE|| aClass == Short.class || aClass ==Short.TYPE|| aClass == Character.class || aClass ==Character.TYPE|| aClass == Long.class || aClass ==Long.TYPE|| aClass == Double.class || aClass ==Double.TYPE|| aClass == Float.class || aClass ==Float.TYPE|| aClass == Boolean.class || aClass ==Boolean.TYPE

){return true;

}else{return false;

}

}/*** 单个对象-属性修改对比

* 属性如果是集合类型,则要求泛型需要重写hashCode和equals方法

*@paramnewObj

*@paramoldObj

*@paramreset 是否重置属性

*@returntrue有变更 false无变更*/

public Boolean objModifyContrast(Object newObj,Object oldObj,booleanreset){boolean flag = false;if (newObj != null && oldObj != null){

Map newMap =getReflexMap(newObj);

Map oldMap =getReflexMap(oldObj);int diff = 0;for(String field : newMap.keySet()) {if (!ignoreFields.contains(field)){

Object newFieldValue=newMap.get(field);

Object oldFieldValue=oldMap.get(field);if (newFieldValue != null && oldFieldValue != null){if(newFieldValue.equals(oldFieldValue)){if(reset){

newMap.put(field,null);

oldMap.put(field,null);

}

}else{

diff++;

}

}else{if (!(newFieldValue == null && oldFieldValue == null)){

diff++;

}

}

}

}if (diff > 0){

setReflexObj(newMap, newObj);

setReflexObj(oldMap, oldObj);

flag= true;

}else{

flag= false;

}

}else{if (newObj == null && oldObj == null){

flag= false;

}else{

flag= true;

}

}returnflag;

}/*** 获取对象的属性名 + 属性值

*@paramobj

*@return

*/

public MapgetReflexMap(Object obj){

Map map = new HashMap<>();

Class extends Object> aClass =obj.getClass();

Field[] declaredFields=aClass.getDeclaredFields();for(Field declaredField : declaredFields) {

declaredField.setAccessible(true);

String fieldName=declaredField.getName();

Object fieldValue= null;try{

fieldValue=declaredField.get(obj);

}catch(IllegalAccessException e) {

e.printStackTrace();

}

map.put(fieldName,fieldValue);

}returnmap;

}/*** 设置 对象的 属性名+属性值

*@parammap

*@return

*/

public Object setReflexObj(Mapmap,Object obj){

Set fieldSet =map.keySet();

Class extends Object> aClass =obj.getClass();for(String field : fieldSet) {try{

Field declaredField=aClass.getDeclaredField(field);

declaredField.setAccessible(true);

declaredField.set(obj,map.get(field));

}catch(NoSuchFieldException e) {

e.printStackTrace();

}catch(IllegalAccessException e) {

e.printStackTrace();

}

}returnobj;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值