Java 如何返回两个对象的差
我正在编写一个Java程序,需要比较两个db对象并返回差异 对象如下所示:Java 如何返回两个对象的差,java,Java,我正在编写一个Java程序,需要比较两个db对象并返回差异 对象如下所示: { "_id" : ObjectId("5dba8987b4a39c13bc104a23"), "contact" : { "firstName" : "Mark", "lastName" : "Doe", "parentsBloodType" : [ { "type" : "AB+",
{
"_id" : ObjectId("5dba8987b4a39c13bc104a23"),
"contact" : {
"firstName" : "Mark",
"lastName" : "Doe",
"parentsBloodType" : [
{
"type" : "AB+",
},
{
"type" : "A+",
},
],
},
"createdAt" : ISODate("2019-10-31T07:13:11.278Z"),
"updatedAt" : ISODate("2019-11-26T09:59:41.611Z")
},
{
"_id" : ObjectId("5dba8987b4a39c13bc104a24"),
"contact" : {
"firstName" : "David",
"lastName" : "Doe",
"parentsBloodType" : [
{
"type" : "B+",
},
{
"type" : "A+",
},
],
},
"createdAt" : ISODate("2019-10-31T07:13:12.278Z"),
"updatedAt" : ISODate("2019-11-26T09:59:42.611Z")
}
结果的格式并不重要,只要它告诉我名称和其中一种血型不同,并且值(可以是字符串或HashMap)
我尝试将其转换为字符串,将字符串拆分为HashMap,并比较键和值,但是由于对象中的数组,字符串的拆分并不顺利
可以直接比较两个对象吗?我认为可以使用contains函数和覆盖pojo中的equals方法来比较arraylist。 所以在你的主课上写 yourlistarray.conatins(pojo)//将返回true或false 在您的pojo类中,根据您的参数(在您的案例名称中)使用以下代码
假设:您有一个有效的JSON,如下所示:
{
"_id": "5dba8987b4a39c13bc104a23",
"contact": {
"firstName": "Mark",
"lastName": "Doe",
"parentsBloodType": [{
"type": "AB+"
},
{
"type": "A+"
}
]
},
"createdAt": "2019-10-31T07:13:11.278Z",
"updatedAt": "2019-11-26T09:59:41.611Z"
}
您可以根据json模式创建Java对象。那可能看起来像这里
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Example {
@JsonProperty("_id")
private String id;
@JsonProperty("contact")
private Contact contact;
@JsonProperty("createdAt")
private String createdAt;
@JsonProperty("updatedAt")
private String updatedAt;
@JsonProperty("_id")
public String getId() {
return id;
}
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}
@JsonProperty("contact")
public Contact getContact() {
return contact;
}
@JsonProperty("contact")
public void setContact(Contact contact) {
this.contact = contact;
}
@JsonProperty("createdAt")
public String getCreatedAt() {
return createdAt;
}
@JsonProperty("createdAt")
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
@JsonProperty("updatedAt")
public String getUpdatedAt() {
return updatedAt;
}
@JsonProperty("updatedAt")
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
现在覆盖这些类的hashcode和equals方法。然后,您就可以根据所需的密钥筛选出记录。这些对象似乎来自mongo db。 因此,最好的方法是直接从java调用mongo并将对象转换为java对象,然后像在java中通常做的那样实现equals,或者如果希望将对象与自定义逻辑进行比较,则使用Comparator 如果这是不可能的,并且是您的输入(如您的程序将这些文件作为输入读取),则您可以:
\u id
的mongo特定内容:“5dba8987b4a39c13bc104a23”(对于第一个对象),如果需要比较,请使用实际日期替换ISODate,或将行全部删除。
从技术上讲,您可以使用正则表达式来实现这一点
公共类MYObject{
私有字符串_id;
私人接触;
私有LocalDateTime createdAt,updatedAt;
}
公共类联系人{
字符串firstName,lastName;
列出parentsBloodTypes;
}
公共类父血型{
私有字符串类型;
}
你所说的两个对象是Java对象,对吧?它们不是你在这里展示的字符串,对吧?你在这里展示的只是一个表示,对吧?如果可能的话,我会将这些数据重新格式化为有效的JSON(几乎是)然后使用许多非常好的JSON库中的一个将其映射到Java类,就像GSON一样。然后,您可以轻松地访问您想要的所有数据。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Example {
@JsonProperty("_id")
private String id;
@JsonProperty("contact")
private Contact contact;
@JsonProperty("createdAt")
private String createdAt;
@JsonProperty("updatedAt")
private String updatedAt;
@JsonProperty("_id")
public String getId() {
return id;
}
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}
@JsonProperty("contact")
public Contact getContact() {
return contact;
}
@JsonProperty("contact")
public void setContact(Contact contact) {
this.contact = contact;
}
@JsonProperty("createdAt")
public String getCreatedAt() {
return createdAt;
}
@JsonProperty("createdAt")
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
@JsonProperty("updatedAt")
public String getUpdatedAt() {
return updatedAt;
}
@JsonProperty("updatedAt")
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact {
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("parentsBloodType")
private List<ParentsBloodType> parentsBloodType = null;
@JsonProperty("firstName")
public String getFirstName() {
return firstName;
}
@JsonProperty("firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty("lastName")
public String getLastName() {
return lastName;
}
@JsonProperty("lastName")
public void setLastName(String lastName) {
this.lastName = lastName;
}
@JsonProperty("parentsBloodType")
public List<ParentsBloodType> getParentsBloodType() {
return parentsBloodType;
}
@JsonProperty("parentsBloodType")
public void setParentsBloodType(List<ParentsBloodType> parentsBloodType) {
this.parentsBloodType = parentsBloodType;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ParentsBloodType {
@JsonProperty("type")
private String type;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
}
public class MYObject {
private String _id;
private Contact contact;
private LocalDateTime createdAt, updatedAt;
}
public class Contact {
String firstName, lastName;
List<ParentsBloodType> parentsBloodTypes;
}
public class ParentBloodType {
private String type;
}