查找Java中相同Pojo的两个对象的差异


public static Map<String, Object> difference(Organization current, Organization prev) throws IllegalArgumentException, IllegalAccessException{
Map<String, Object> changedProperties = new HashMap<>();
for(Field field : current.getClass().getDeclaredFields()){
field.setAccessible(true);
Object value1 = field.get(current);
Object value2 = field.get(prev);
if(value1 != null && value2 != null) {
if(value1.getClass() == String.class) { //if property is type of String
if(!Objects.equals(value1, value2)) {
changedProperties.put(field.getName(), (String) value1);
}
}else {
if(!Objects.equals(value1, value2)) {
for(Field cur : value1.getClass().getDeclaredFields()){
for(Field pre : value2.getClass().getDeclaredFields()){
cur.setAccessible(true);
pre.setAccessible(true);

if(cur.getName().equals(pre.getName())) { //compare same type of properties like Contact1 with Contact2
Object val1 = cur.get(value1);
Object val2 = pre.get(value2);

if(val1 == null && val2 == null) {
//Do nothing
}else {
changedProperties.put(cur.getName(), val1);
}
}
}
}
}
}
}
}
return changedProperties;
}

这是我的Pojo

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Organization {
private String unitId;

private String unitType;

private Contact contact; 
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
class Contact{
private Name name;
private Address address;
private List<Phone> phones;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
class Name{
private String brandName;
private String department;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
class Address{
private String addressLine1;
private String addressLine2;
private String postalCode;
private String city;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
class Phone{
private String id;
private String number;
private Boolean primary;
private String comment;
}

我能够得到相同POJO的两个对象的差异,但我需要保持层次结构,这意味着我想要这作为差异:contact= contact (phones=[Phone(id=1, number=46127, primary=true,comment=null)])而不是这样:phones=[Phone(id=1, number=46127,主= true,评论= null)。请帮助。

在Java中,您可以覆盖对象的equals()方法。如果你有属性,这些属性是自定义对象,你可以为这些对象调用equals()方法。例如,在您的组织对象中,您可以创建这个方法。

@Override
public boolean equals(Organization org){
return org.getUnitId() != null && org.getUnitType() != null && org.getContact() != null && org.getUnitId().equals(this.getUnitId()) && org.getUnitType().equals(this.getUnitType) && org.getContact().equals(this.getContact());
}

同样,您需要以相同的方式为Contact创建等号:

@Override
public boolean equals(Contact contact){
//your custom equals for contact, this method is being called in equals() from Organization.
}

,然后在比较组织时,将对嵌套对象进行比较。除了equals,你还可以重写compareTo()。

更多信息在这里:https://www.geeksforgeeks.org/overriding-equals-method-in-java/

你可以这样做:

@Override
public boolean equals(Object other) {
if (!(other instanceof Organization )) {
return false;
}
Organization that = (Organization ) other;
// Custom equality check here.
return this.field1.equals(that.field1)
&& this.field2.equals(that.field2);
}

如果你是存储在哈希表中,你也需要实现hashcode

@Override
public int hashCode() {
int hashCode = 1;
hashCode = hashCode * 37 + this.unitId.hashCode();
hashCode = hashCode * 37 + this.unitTypeId.hashCode();
hashCode = hashCode * 37 + this.contact.getName().hashCode();
return hashCode;
}

最新更新