最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中2個(gè)對(duì)象字段值比較是否相同

 更新時(shí)間:2022年04月14日 09:41:25   作者:碼奴生來只知道前進(jìn)~  
本文主要介紹了Java中2個(gè)對(duì)象字段值比較是否相同,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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("原對(duì)象不能為空");
        }
        if(afterObj == null) {
            throw new RuntimeException("新對(duì)象不能為空");
        }
        if(!beforeObj.getClass().isAssignableFrom(afterObj.getClass())){
            throw new RuntimeException("兩個(gè)對(duì)象不相同,無法比較");
        }
 
        //取出屬性
        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 ?接口請(qǐng)求參數(shù)類
?* @Version 1.0
?* @since 2020-08-03 20:06
?*/
@Data
@Accessors(chain = true) //注解用來配置lombok如何產(chǎn)生和顯示getters和setters的方法
public class ApIData {
?
? ? /**
? ? ?* 身份證號(hào)
? ? ?*/
? ? private String ident_card;
?
? ? /**
? ? ?* 姓名
? ? ?*/
? ? private String name;
?
? ? /**
? ? ?* 戶號(hào) ?水務(wù)局使用查詢
? ? ?*/
? ? private String hh;
?
? ? /**
? ? ?* 用水月份 ?YYYY-MM
? ? ?*/
? ? private String month;
?
? ? /**
? ? ?* 房東用戶ID
? ? ?*/
? ? private String owner_id;
?
? ? /**
? ? ?* 所屬街道
? ? ?*/
? ? private String street_name;

}

到此這篇關(guān)于Java中2個(gè)對(duì)象字段值比較是否相同的文章就介紹到這了,更多相關(guān)Java 對(duì)象字段值比較內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

兴国县| 曲沃县| 鹤峰县| 德昌县| 恭城| 晋城| 阳谷县| 仙桃市| 阿拉善盟| 阿瓦提县| 庆元县| 雷波县| 扬中市| 日照市| 赤水市| 鹤山市| 方城县| 台州市| 旅游| 泾阳县| 乐亭县| 苍梧县| 平遥县| 夏河县| 阿勒泰市| 泽普县| 钟山县| 商丘市| 彰化市| 内江市| 多伦县| 富源县| 汪清县| 信宜市| 长丰县| 云阳县| 太白县| 邵武市| 彰化市| 宜君县| 藁城市|