[英]Comparing Two Pojo List for changes
I have two Pojo Classes DriverPrimary
, DriverSecondary
我有两个 Pojo 类
DriverPrimary
和DriverSecondary
I am comparing Driver Primary with driver secondary and if there are any changes I need to find that out我正在比较 Driver Primary 和 Driver secondary 如果有任何变化我需要找出来
Can you please let me know how it is done?你能告诉我它是怎么做到的吗?
DriverPrimary.java DriverPrimary.java
public class DriverPrimary implements Serializable {
private static final long serialVersionUID = 1L;
private String DriverId;
private String role;
}
DriverSecondary.java 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
SO how to compare the above two list, though the field names are different both have similar values那么如何比较上面的两个列表,虽然字段名称不同但都有相似的值
Edited: Removed Equals and hashcode has that will work with class with same structure.已编辑:删除了 Equals 和 hashcode,它可以与具有相同结构的 class 一起使用。 Edit2: modified the code for better understanding
Edit2:修改代码以便更好地理解
One of the lists should be remapped to the classes of another type, to make the contents of the lists "comparable":其中一个列表应该重新映射到另一种类型的类,以使列表的内容“可比较”:
// 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>
Then, assuming that equals
and hashCode
are implemented properly in DriverPrimary
, the difference between the two lists may be found using for example List::removeAll
(or just List::equals
).然后,假设
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.