java 对象属性比较工具类
你猜
阅读:724
2021-03-31 21:14:47
评论:0
package com.zxy.product.human.web.log;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Maps;
/**
* BeanUtil 对象属性比较工具类
* @author zzg
*
**/
public class BeanUtil {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
BeanUtil util = new BeanUtil();
//excel 读取数据
Person excel = util.new Person();
excel.setId("1");
excel.setAge("18");
excel.setName("知学云1");
//database 读取数据
Person database = util.new Person();
database.setId("1");
database.setAge("16");
database.setName("知学云");
Map<String,Map<String,Object>> map = util.getMap(excel);
//遍历map
// for (Map.Entry<String,Object> entry : map.entrySet()) {
// System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
// }
boolean flag = util.getFieldEqual(database, map);
System.out.println("属性值是否发生改变:"+ flag);
Map<String,Map<String,Object>> fields = util.getFieldUpdate(database, map);
Map<String,Object> c = fields.get(database.getId());
for (Map.Entry<String,Object> entry : c.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
// 指定Bean对象 关联属性转换为Map
public Map<String,Map<String,Object>> getMap(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> obj = new HashMap<String,Map<String,Object>>();
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
obj.put(object.getId(), map);
return obj;
}
// Bean 关联属性转换为Map
public Map<String,Object> getMapProperties(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
return map;
}
// 获取指定Field 属性值
public Object getFieldValue(Object obj, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Class<?> cls = obj.getClass();
Object value = null;
Field field = cls.getDeclaredField(fieldName);;
field.setAccessible(true);
value = field.get(obj);
return value;
}
//判断指定对象的属性值是否相等
public boolean getFieldEqual(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean flag = false;
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
if(map.containsKey(object.getId())){
Map<String, Object> content = map.get(object.getId());
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
if(excel == null && database == null){
flag = false;
}else if(excel != null && database == null){
flag = true;
break;
}else if(excel == null && database != null){
flag = true;
break;
}else if(excel != null && database != null){
if(excel.equals(database)){
flag = false;
}else{
flag = true;
break;
}
}
}
}
return flag;
}
// 返回匹配对象修改的属性值
public Map<String,Map<String,Object>> getFieldUpdate(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> result =new HashMap<String,Map<String,Object>>();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
Map<String, Object> content = map.get(object.getId());
if(map.containsKey(object.getId())){
Map<String,Object> f = new HashMap<String,Object>();
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
//属性值判断:四种情况
if(excel != null && database!= null){
if(!excel.equals(database)){
f.put(field.getName(), excel);
}
}else if(excel == null && database == null){
}else if(excel != null && database == null){
f.put(field.getName(), excel);
}else if(excel == null && database != null){
f.put(field.getName(), null);
}
}
result.put(object.getId(), f);
}
return result;
}
public class Person{
private String id;
private String name;
private String age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
}
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Maps;
/**
* BeanUtil 对象属性比较工具类
* @author zzg
*
**/
public class BeanUtil {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
BeanUtil util = new BeanUtil();
//excel 读取数据
Person excel = util.new Person();
excel.setId("1");
excel.setAge("18");
excel.setName("知学云1");
//database 读取数据
Person database = util.new Person();
database.setId("1");
database.setAge("16");
database.setName("知学云");
Map<String,Map<String,Object>> map = util.getMap(excel);
//遍历map
// for (Map.Entry<String,Object> entry : map.entrySet()) {
// System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
// }
boolean flag = util.getFieldEqual(database, map);
System.out.println("属性值是否发生改变:"+ flag);
Map<String,Map<String,Object>> fields = util.getFieldUpdate(database, map);
Map<String,Object> c = fields.get(database.getId());
for (Map.Entry<String,Object> entry : c.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
// 指定Bean对象 关联属性转换为Map
public Map<String,Map<String,Object>> getMap(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> obj = new HashMap<String,Map<String,Object>>();
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
obj.put(object.getId(), map);
return obj;
}
// Bean 关联属性转换为Map
public Map<String,Object> getMapProperties(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
return map;
}
// 获取指定Field 属性值
public Object getFieldValue(Object obj, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Class<?> cls = obj.getClass();
Object value = null;
Field field = cls.getDeclaredField(fieldName);;
field.setAccessible(true);
value = field.get(obj);
return value;
}
//判断指定对象的属性值是否相等
public boolean getFieldEqual(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean flag = false;
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
if(map.containsKey(object.getId())){
Map<String, Object> content = map.get(object.getId());
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
if(excel == null && database == null){
flag = false;
}else if(excel != null && database == null){
flag = true;
break;
}else if(excel == null && database != null){
flag = true;
break;
}else if(excel != null && database != null){
if(excel.equals(database)){
flag = false;
}else{
flag = true;
break;
}
}
}
}
return flag;
}
// 返回匹配对象修改的属性值
public Map<String,Map<String,Object>> getFieldUpdate(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> result =new HashMap<String,Map<String,Object>>();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
Map<String, Object> content = map.get(object.getId());
if(map.containsKey(object.getId())){
Map<String,Object> f = new HashMap<String,Object>();
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
//属性值判断:四种情况
if(excel != null && database!= null){
if(!excel.equals(database)){
f.put(field.getName(), excel);
}
}else if(excel == null && database == null){
}else if(excel != null && database == null){
f.put(field.getName(), excel);
}else if(excel == null && database != null){
f.put(field.getName(), null);
}
}
result.put(object.getId(), f);
}
return result;
}
public class Person{
private String id;
private String name;
private String age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。