例如下述Java类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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 "当前拓扑的PK为:" + topology_pk + ",ID为:" + topology_id; } } |
如下想判断下面两个对象中全部属性值是否一致时,有哪些办法呢?
1
2
3
4
5
6
|
Bean_Topology topology1 = new Bean_Topology(); topology1.setTopology_id( "1" ); Bean_Topology topology2 = new Bean_Topology(); topology2.setTopology_pk(1L); topology2.setTopology_id( "1" ); |
方法一:重写Bean_Topology的equals方法和hashcode方法,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@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(); } |
测试代码如下:
1
2
3
4
5
|
if (topology1.equals(topology2)) { System.out.println( "对象1与对象2的属性值无差异。" ); } else { System.out.println( "对象1与对象2的属性值有差异。" ); } |
输出结果为:
对象1与对象2的属性值有差异。
方法二:调用文章末尾的工具类,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Map<String, Map<String,Object>> resultMap=compareFields(topology1,topology2); int size=resultMap.size(); if (size> 0 ) { System.out.println( "对象1与对象2的属性值有差异,差异结果如下:" ); 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( "对象1与对象2的属性值无差异!" ); } |
输出结果为:
对象1与对象2的属性值有差异,差异结果如下:
topology_pk(oldValue:null,newValue:1)
工具类如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
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 { /** * 比较两个实体属性值,返回一个boolean,true则表时两个对象中的属性值无差异 * @param oldObject 进行属性比较的对象1 * @param newObject 进行属性比较的对象2 * @return 属性差异比较结果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 ; } } /** * 比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个Map分别存oldObject,newObject此属性名的值 * @param oldObject 进行属性比较的对象1 * @param newObject 进行属性比较的对象2 * @return 属性差异比较结果map */ @SuppressWarnings ( "rawtypes" ) public static Map<String, Map<String,Object>> compareFields(Object oldObject, Object newObject) { Map<String, Map<String, Object>> map = null ; try { /** * 只有两个对象都是同一类型的才有可比性 */ 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上调用get方法等同于获得oldObject的属性值 Object oldValue = readMethod.invoke(oldObject); // 在newObject上调用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)) { // 比较这两个值是否相等,不等就可以放入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。
到此这篇关于Java比较两个对象中全部属性值是否相等的方法的文章就介绍到这了,更多相关Java比较对象属性值内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://xiaoer.blog.csdn.net/article/details/85005295