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

Java比較兩個(gè)對(duì)象中全部屬性值是否相等的方法

 更新時(shí)間:2021年08月06日 15:49:24   作者:王小二(海闊天空)  
本文主要介紹了Java比較兩個(gè)對(duì)象中全部屬性值是否相等的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

例如下述Java類:

import java.io.Serializable;
import java.util.List;

public class Bean_Topology implements Serializable {
 private static final long serialVersionUID = 1L;
 public static long getSerialversionuid() {
  return serialVersionUID;
 }

 private Long topology_pk;

 private String topology_id;

 public String getTopology_id() {
  return topology_id;
 }

 public void setTopology_id(String topology_id) {
  this.topology_id = topology_id;
 }

 public Long getTopology_pk() {
  return topology_pk;
 }

 public void setTopology_pk(Long topology_pk) {
  this.topology_pk = topology_pk;
 }

 @Override
 public String toString() {
  return "當(dāng)前拓?fù)涞腜K為:" + topology_pk + ",ID為:" + topology_id;
 }
}

如下想判斷下面兩個(gè)對(duì)象中全部屬性值是否一致時(shí),有哪些辦法呢?

Bean_Topology topology1 = new Bean_Topology();
 topology1.setTopology_id("1");

Bean_Topology topology2 = new Bean_Topology();
 topology2.setTopology_pk(1L);
 topology2.setTopology_id("1");

方法一:重寫B(tài)ean_Topology的equals方法和hashcode方法,代碼如下:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
  
    if (obj == null || getClass() != obj.getClass()) {
        return false;
     }

    Bean_Topology topology = (Bean_Topology) obj;
    if (topology_pk == null) {
        if (topology.topology_pk != null) {
            return false;
        } 
    }else if (!topology_pk.equals(topology.topology_pk)) {
        return false; 
    }
        
    if (topology_id == null) {
        if (topology.topology_id != null) {
            return false;
        } 
    }else if (!topology_id.equals(topology.topology_id)) {
        return false; 
    }
        
        return true;
    }
 
    @Override
    public int hashCode() {
        return  topology_pk.hashCode()+topology_id.hashCode();
    }

測(cè)試代碼如下:

if(topology1.equals(topology2)) {
 System.out.println("對(duì)象1與對(duì)象2的屬性值無差異。");
}else {
 System.out.println("對(duì)象1與對(duì)象2的屬性值有差異。"); 
}

輸出結(jié)果為:

對(duì)象1與對(duì)象2的屬性值有差異。

方法二:調(diào)用文章末尾的工具類,代碼如下:

Map<String, Map<String,Object>> resultMap=compareFields(topology1,topology2);
int size=resultMap.size();
if(size>0) {
    System.out.println("對(duì)象1與對(duì)象2的屬性值有差異,差異結(jié)果如下:");
  
    Iterator<String> it = resultMap.keySet().iterator();
    while(it.hasNext()) {
        String key=it.next();
        System.out.println("  "+key+"(oldValue:"+resultMap.get(key).get("oldValue")+",newValue:"+resultMap.get(key).get("newValue")+")");
        } 
}else {
 System.out.println("對(duì)象1與對(duì)象2的屬性值無差異!");
}

輸出結(jié)果為:

對(duì)象1與對(duì)象2的屬性值有差異,差異結(jié)果如下:
  topology_pk(oldValue:null,newValue:1)

工具類如下:

package com.sitech.modual.util.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.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sitech.modual.bean.Bean_Link;
import com.sitech.modual.bean.Bean_Topology;

public class ClassCompareUtil {
 /**
  * 比較兩個(gè)實(shí)體屬性值,返回一個(gè)boolean,true則表時(shí)兩個(gè)對(duì)象中的屬性值無差異
  * @param oldObject 進(jìn)行屬性比較的對(duì)象1
  * @param newObject 進(jìn)行屬性比較的對(duì)象2
  * @return 屬性差異比較結(jié)果boolean
  */
 public static boolean compareObject(Object oldObject, Object newObject) {
  Map<String, Map<String,Object>> resultMap=compareFields(oldObject,newObject);
  
  if(resultMap.size()>0) {
   return false;
  }else {
   return true;
  }
 }
 
 /**
  * 比較兩個(gè)實(shí)體屬性值,返回一個(gè)map以有差異的屬性名為key,value為一個(gè)Map分別存oldObject,newObject此屬性名的值
  * @param oldObject 進(jìn)行屬性比較的對(duì)象1
  * @param newObject 進(jìn)行屬性比較的對(duì)象2
  * @return 屬性差異比較結(jié)果map
  */
 @SuppressWarnings("rawtypes")
 public static Map<String, Map<String,Object>> compareFields(Object oldObject, Object newObject) {
  Map<String, Map<String, Object>> map = null;
  
  try{ 
   /**
    * 只有兩個(gè)對(duì)象都是同一類型的才有可比性
    */
   if (oldObject.getClass() == newObject.getClass()) {
    map = new HashMap<String, Map<String,Object>>();
    
    Class clazz = oldObject.getClass();
    //獲取object的所有屬性
    PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,Object.class).getPropertyDescriptors();
    
    for (PropertyDescriptor pd : pds) {
     //遍歷獲取屬性名
     String name = pd.getName();
     
     //獲取屬性的get方法
     Method readMethod = pd.getReadMethod();
     
     // 在oldObject上調(diào)用get方法等同于獲得oldObject的屬性值
     Object oldValue = readMethod.invoke(oldObject);
     // 在newObject上調(diào)用get方法等同于獲得newObject的屬性值
     Object newValue = readMethod.invoke(newObject);
     
     if(oldValue instanceof List){
      continue;
     }
     
     if(newValue instanceof List){
      continue;
     }
     
     if(oldValue instanceof Timestamp){
      oldValue = new Date(((Timestamp) oldValue).getTime());
     }
     
     if(newValue instanceof Timestamp){
      newValue = new Date(((Timestamp) newValue).getTime());
     }
     
     if(oldValue == null && newValue == null){
      continue;
     }else if(oldValue == null && newValue != null){
      Map<String,Object> valueMap = new HashMap<String,Object>();
       valueMap.put("oldValue",oldValue);
       valueMap.put("newValue",newValue);
      
      map.put(name, valueMap);
      
      continue;
     }
     
     if (!oldValue.equals(newValue)) {// 比較這兩個(gè)值是否相等,不等就可以放入map了
      Map<String,Object> valueMap = new HashMap<String,Object>();
       valueMap.put("oldValue",oldValue);
       valueMap.put("newValue",newValue);
      
      map.put(name, valueMap);
     }
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
  
  return map;
 }
}

注意:本工具類不適用于比較包含List,Map等類的Class。

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

相關(guān)文章

  • SpringBoo項(xiàng)目打war包的多種方式

    SpringBoo項(xiàng)目打war包的多種方式

    在idea中,經(jīng)常會(huì)對(duì)web項(xiàng)目進(jìn)行打jar包或者war包,jar包在Java環(huán)境中運(yùn)行,war包在Tomcat服務(wù)器中跑,對(duì)于打war包有多種方式,一下介紹3鐘方式,需要的朋友可以參考下
    2024-06-06
  • Java定時(shí)器@Scheduled注解的使用方法

    Java定時(shí)器@Scheduled注解的使用方法

    這篇文章主要給大家介紹了關(guān)于Java定時(shí)器@Scheduled注解的使用方法,在Java中使用@Scheduled注解可以方便地實(shí)現(xiàn)定時(shí)任務(wù)的功能,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場景的三種方法

    MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場景的三種方法

    MyBatis Plus提供了多種簡便的方式來進(jìn)行一對(duì)多子查詢,本文主要介紹了MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場景的三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • Java web中 war exploded 的解決方案

    Java web中 war exploded 的解決方案

    這篇文章主要介紹了Java web中 war exploded 的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Windows下 IDEA編譯調(diào)試 hive2.3.9的過程解析

    Windows下 IDEA編譯調(diào)試 hive2.3.9的過程解析

    這篇文章主要介紹了Windows下 IDEA編譯調(diào)試 hive2.3.9的過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • SpringBoot實(shí)戰(zhàn)之處理異常案例詳解

    SpringBoot實(shí)戰(zhàn)之處理異常案例詳解

    這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)之處理異常案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • SSM框架中測(cè)試單元的使用 spring整合Junit過程詳解

    SSM框架中測(cè)試單元的使用 spring整合Junit過程詳解

    這篇文章主要介紹了SSM框架中測(cè)試單元的使用 spring整合Junit過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Spring IOC 能降低耦合的問題分析及解決方法

    Spring IOC 能降低耦合的問題分析及解決方法

    這篇文章主要介紹了Spring IOC 為什么能降低耦合,依賴注入是調(diào)用者僅通過聲明某個(gè)組件就可以獲得組件的控制權(quán),而對(duì)該組件的依賴關(guān)系管理、查找、加載由外部完成,需要的朋友可以參考下
    2022-06-06
  • Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文)

    Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文)

    這篇文章主要介紹了Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • 解析SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能

    解析SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能

    這篇文章主要介紹了SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能,AOP面向切面編程,通過為目標(biāo)類織入切面的方式,實(shí)現(xiàn)對(duì)目標(biāo)類功能的增強(qiáng),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09

最新評(píng)論

合江县| 恭城| 新建县| 昭觉县| 英德市| 金堂县| 靖西县| 孟州市| 德保县| 延津县| 绥棱县| 台东市| 沁水县| 深水埗区| 青田县| 兰坪| 延津县| 石柱| 巴南区| 常熟市| 深水埗区| 纳雍县| 星子县| 错那县| 凤阳县| 蓝田县| 宝丰县| 彰化市| 宜良县| 桦甸市| 天峨县| 奎屯市| 佳木斯市| 新竹县| 松阳县| 红原县| 法库县| 洛宁县| 宜君县| 枣强县| 乐安县|