溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中2個對象字段值怎么比較是否相同

發(fā)布時間:2022-04-14 13:46:40 來源:億速云 閱讀:693 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Java中2個對象字段值怎么比較是否相同的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java中2個對象字段值怎么比較是否相同文章都會有所收獲,下面我們一起來看看吧。

工具類

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;
 
    /**
     * 戶號  水務(wù)局使用查詢
     */
    private String hh;
 
    /**
     * 用水月份  YYYY-MM
     */
    private String month;
 
    /**
     * 房東用戶ID
     */
    private String owner_id;
 
    /**
     * 所屬街道
     */
    private String street_name;

}

關(guān)于“Java中2個對象字段值怎么比較是否相同”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Java中2個對象字段值怎么比較是否相同”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI