Java中2個對象字段值比較是否相同
更新時間:2022年04月14日 09:41:25 作者:碼奴生來只知道前進~
本文主要介紹了Java中2個對象字段值比較是否相同,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1、工具類
package com.shucha.deveiface.biz.utils;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2022-03-21 16:50
*/
import com.shucha.deveiface.biz.model.Comparison;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class CompareObjUtil {
public static List<Comparison> compareObj(Object beforeObj, Object afterObj) throws Exception{
List<Comparison> diffs = new ArrayList<>();
if(beforeObj == null) {
throw new RuntimeException("原對象不能為空");
}
if(afterObj == null) {
throw new RuntimeException("新對象不能為空");
}
if(!beforeObj.getClass().isAssignableFrom(afterObj.getClass())){
throw new RuntimeException("兩個對象不相同,無法比較");
}
//取出屬性
Field[] beforeFields = beforeObj.getClass().getDeclaredFields();
Field[] afterFields = afterObj.getClass().getDeclaredFields();
Field.setAccessible(beforeFields, true);
Field.setAccessible(afterFields, true);
//遍歷取出差異值
if(beforeFields != null && beforeFields.length > 0){
for(int i=0; i<beforeFields.length; i++){
Object beforeValue = beforeFields[i].get(beforeObj);
Object afterValue = afterFields[i].get(afterObj);
if((beforeValue != null && !"".equals(beforeValue) && !beforeValue.equals(afterValue)) || ((beforeValue == null || "".equals(beforeValue)) && afterValue != null)){
Comparison comparison = new Comparison();
comparison.setField(beforeFields[i].getName());
comparison.setBefore(beforeValue);
comparison.setAfter(afterValue);
comparison.setIsUpdate(true);
diffs.add(comparison);
}
}
}
return diffs;
}
}
public static void main(String[] args) throws Exception {
ApIData apIData = new ApIData()
.setName("張三")
.setMonth("5")
.setHh("1");
ApIData apIData1 = new ApIData()
.setName("張三")
.setMonth("9")
.setHh("35");
List<Comparison> list = CompareObjUtil.compareObj(apIData, apIData1);
System.out.println(list);
}
package com.shucha.deveiface.biz.model;
?
import lombok.Data;
import lombok.experimental.Accessors;
?
/**
?* @author tqf
?* @Description ?接口請求參數(shù)類
?* @Version 1.0
?* @since 2020-08-03 20:06
?*/
@Data
@Accessors(chain = true) //注解用來配置lombok如何產(chǎn)生和顯示getters和setters的方法
public class ApIData {
?
? ? /**
? ? ?* 身份證號
? ? ?*/
? ? private String ident_card;
?
? ? /**
? ? ?* 姓名
? ? ?*/
? ? private String name;
?
? ? /**
? ? ?* 戶號 ?水務局使用查詢
? ? ?*/
? ? private String hh;
?
? ? /**
? ? ?* 用水月份 ?YYYY-MM
? ? ?*/
? ? private String month;
?
? ? /**
? ? ?* 房東用戶ID
? ? ?*/
? ? private String owner_id;
?
? ? /**
? ? ?* 所屬街道
? ? ?*/
? ? private String street_name;
}到此這篇關于Java中2個對象字段值比較是否相同的文章就介紹到這了,更多相關Java 對象字段值比較內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
IDEA使用Gradle構建SpringBoot項目工程的詳細教程
這篇文章主要介紹了IDEA使用Gradle構建SpringBoot項目工程的教程詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot用@Async注解實現(xiàn)異步任務
這篇文章主要介紹了SpringBoot用@Async注解實現(xiàn)異步任務,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
Java之關于基本數(shù)據(jù)類型和引用數(shù)據(jù)類型的存放位置
這篇文章主要介紹了Java之關于基本數(shù)據(jù)類型和引用數(shù)據(jù)類型的存放位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
java數(shù)學類Math?BigInteger?BigDecimal使用介紹
這篇文章主要為大家介紹了java數(shù)學類Math、BigInteger、BigDecimal的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
mybatis參數(shù)類型不匹配錯誤argument type mismatch的處理方案
這篇文章主要介紹了mybatis參數(shù)類型不匹配錯誤argument type mismatch的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

