比较两个 Pojo 列表的变化
Comparing Two Pojo List for changes
我有两个 Pojo 类
DriverPrimary
、DriverSecondary
我正在比较主要驱动程序和次要驱动程序,如果有任何变化,我需要找出来
你能告诉我这是怎么做到的吗?
DriverPrimary.java
public class DriverPrimary implements Serializable {
private static final long serialVersionUID = 1L;
private String DriverId;
private String role;
}
DriverSecondary.java
public class DriverSecondary{
private String driverSecondaryDriverType;
private String driverSecondaryDriverId;
}
List<DriverPrimary> driverPrimary = new ArrayList<DriverPrimary>();
List<DriverSecondary> driverSecondary = new ArrayList<DriverSecondary>();
driverPrimary=DataEBO.getDriverPrimary();// from datasource which is not empty and has values
driverSecondary=DataDTO.getDriverSecondary();// from datasource which is not empty and has values
所以如何比较以上两个列表,虽然字段名称不同但两者的值相似
已编辑:
删除了 Equals 和 hashcode,它可以与具有相同结构的 class 一起使用。
Edit2:修改代码以便更好地理解
其中一个列表应重新映射到另一种类型的 类,以使列表的内容“具有可比性”:
// from datasource which is not empty and has values
List<DriverPrimary> primary = DataEBO.getDriverPrimary();
List<DriverPrimary> secondary = DataDTO.getDriverSecondary() // List<DriverSecondary>
.stream()
.map(sec -> new DriverPrimary(
sec.getDriverSecondaryDriverId(), // id -> id
sec.getDriverSecondaryDriverType() // type -> role
))
.collect(Collectors.toList()); // List<DriverPrimary>
然后,假设 equals
和 hashCode
在 DriverPrimary
中正确实现,可以使用例如 List::removeAll
(或者只是List::equals
).
List<DriverPrimary> diff1 = new ArrayList<>(primary);
diff1.removeAll(secondary); // items in primary missing in secondary
List<DriverPrimary> diff2 = new ArrayList<>(secondary);
diff2.removeAll(primary); // items in secondary missing in primary
我有两个 Pojo 类
DriverPrimary
、DriverSecondary
我正在比较主要驱动程序和次要驱动程序,如果有任何变化,我需要找出来
你能告诉我这是怎么做到的吗?
DriverPrimary.java
public class DriverPrimary implements Serializable {
private static final long serialVersionUID = 1L;
private String DriverId;
private String role;
}
DriverSecondary.java
public class DriverSecondary{
private String driverSecondaryDriverType;
private String driverSecondaryDriverId;
}
List<DriverPrimary> driverPrimary = new ArrayList<DriverPrimary>();
List<DriverSecondary> driverSecondary = new ArrayList<DriverSecondary>();
driverPrimary=DataEBO.getDriverPrimary();// from datasource which is not empty and has values
driverSecondary=DataDTO.getDriverSecondary();// from datasource which is not empty and has values
所以如何比较以上两个列表,虽然字段名称不同但两者的值相似
已编辑: 删除了 Equals 和 hashcode,它可以与具有相同结构的 class 一起使用。 Edit2:修改代码以便更好地理解
其中一个列表应重新映射到另一种类型的 类,以使列表的内容“具有可比性”:
// from datasource which is not empty and has values
List<DriverPrimary> primary = DataEBO.getDriverPrimary();
List<DriverPrimary> secondary = DataDTO.getDriverSecondary() // List<DriverSecondary>
.stream()
.map(sec -> new DriverPrimary(
sec.getDriverSecondaryDriverId(), // id -> id
sec.getDriverSecondaryDriverType() // type -> role
))
.collect(Collectors.toList()); // List<DriverPrimary>
然后,假设 equals
和 hashCode
在 DriverPrimary
中正确实现,可以使用例如 List::removeAll
(或者只是List::equals
).
List<DriverPrimary> diff1 = new ArrayList<>(primary);
diff1.removeAll(secondary); // items in primary missing in secondary
List<DriverPrimary> diff2 = new ArrayList<>(secondary);
diff2.removeAll(primary); // items in secondary missing in primary