java 实体比较_java比较实体类
展開(kāi)全部
這種情況你可以使用反射去寫(xiě)一個(gè)通用的方法,不過(guò)前提是你的32313133353236313431303231363533e4b893e5b19e31333363393664那些實(shí)體類都是簡(jiǎn)單的包裝類,稍后奉上代碼。代碼如下:package?pojo;
/**
*?Created?by?liaokai?on?2017/11/17?0017.
*/
public?class?Person?{
private?String?name;
private?int?age;
public?Person(String?name,?int?age)?{
this.name?=?name;
this.age?=?age;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
@Override
public?boolean?equals(Object?o)?{
if?(this?==?o)?return?true;
if?(o?==?null?||?getClass()?!=?o.getClass())?return?false;
Person?person?=?(Person)?o;
if?(age?!=?person.age)?return?false;
return?name?!=?null???name.equals(person.name)?:?person.name?==?null;
}
@Override
public?int?hashCode()?{
int?result?=?name?!=?null???name.hashCode()?:?0;
result?=?31?*?result?+?age;
return?result;
}
@Override
public?String?toString()?{
return?"Person{"?+
"name='"?+?name?+?'\''?+
",?age="?+?age?+
'}';
}
}
package?util;
import?pojo.Person;
import?java.lang.reflect.Field;
import?java.util.ArrayList;
import?java.util.HashMap;
import?java.util.List;
import?java.util.Map;
/**
*?Created?by?liaokai?on?2017/11/17?0017.
*/
public?class?CompareUtil?{
public?List?compareT(T?t1,T?t2,String?className){
List?list?=?new?ArrayList();
//內(nèi)容沒(méi)改變直接返回
if(t1.equals(t2)){
return?list;
}
try?{
Class?c?=?Class.forName(className);
//讀取t1和t2中的所有屬性
Field[]?fields?=?c.getDeclaredFields();
for?(int?i?=?0;?i?
Field?field?=fields[i];
field.setAccessible(true);
Object?value1=field.get(t1);
Object?value2=field.get(t2);
//判斷這兩個(gè)值是否相等
if(!isValueEquals(value1,value2)){
Map?map?=?new?HashMap();
map.put("name",field.getName());
map.put("before",value1);
map.put("after",value2);
list.add(map);
}
}
}?catch?(ClassNotFoundException?e)?{
e.printStackTrace();
}?catch?(IllegalAccessException?e)?{
e.printStackTrace();
}
return?list;
}
private?boolean?isValueEquals(Object?value1,?Object?value2)?{
if(value1==null&&value2==null){
return?true;
}
if(value1==null&&value2!=null){
return?false;
}
if(value1.equals(value2)){
return?true;
}
return?false;
}
}
package?pojo;
import?util.CompareUtil;
/**
*?Created?by?liaokai?on?2017/11/17?0017.
*/
public?class?Test?{
public?static?void?main(String[]?args)?{
Person?p1?=?new?Person("張三",14);
Person?p2?=?new?Person("張三",15);
Person?p3?=?new?Person("李四",15);
CompareUtil?compareUtil?=?new?CompareUtil();
System.out.println(compareUtil.compareT(p1,p1,Person.class.getName()));
System.out.println(compareUtil.compareT(p1,p2,Person.class.getName()));
System.out.println(compareUtil.compareT(p1,p3,Person.class.getName()));
System.out.println(compareUtil.compareT(p2,p3,Person.class.getName()));
}
}
//運(yùn)行結(jié)果
[]
[{before=14,?name=age,?after=15}]
[{before=張三,?name=name,?after=李四},?{before=14,?name=age,?after=15}]
[{before=張三,?name=name,?after=李四}]
總結(jié)
以上是生活随笔為你收集整理的java 实体比较_java比较实体类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 炒股追热点能不能赚钱 追高会站到半山腰
- 下一篇: 辛巴宣布复出 再给网红带货一次机会