Java比較兩個(gè)對(duì)象中全部屬性值是否相等的方法
例如下述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");
方法一:重寫(xiě)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的屬性值無(wú)差異。");
}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的屬性值無(wú)差異!");
}
輸出結(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ì)象中的屬性值無(wú)差異
* @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)文章
MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場(chǎng)景的三種方法
MyBatis Plus提供了多種簡(jiǎn)便的方式來(lái)進(jìn)行一對(duì)多子查詢,本文主要介紹了MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場(chǎng)景的三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Windows下 IDEA編譯調(diào)試 hive2.3.9的過(guò)程解析
這篇文章主要介紹了Windows下 IDEA編譯調(diào)試 hive2.3.9的過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot實(shí)戰(zhàn)之處理異常案例詳解
這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)之處理異常案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SSM框架中測(cè)試單元的使用 spring整合Junit過(guò)程詳解
這篇文章主要介紹了SSM框架中測(cè)試單元的使用 spring整合Junit過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Spring IOC 能降低耦合的問(wèn)題分析及解決方法
這篇文章主要介紹了Spring IOC 為什么能降低耦合,依賴注入是調(diào)用者僅通過(guò)聲明某個(gè)組件就可以獲得組件的控制權(quán),而對(duì)該組件的依賴關(guān)系管理、查找、加載由外部完成,需要的朋友可以參考下2022-06-06
Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文)
這篇文章主要介紹了Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
解析SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能
這篇文章主要介紹了SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能,AOP面向切面編程,通過(guò)為目標(biāo)類織入切面的方式,實(shí)現(xiàn)對(duì)目標(biāo)類功能的增強(qiáng),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

