比较两个对象列表并创建一个List<Map<String, obj>
,其中每个地图都有关键字&#34; old&#34;值为oldObj和key&#34; new&#34;值为newObj
例如:第一个对象列表是 - &gt; List<Company>
(更新列表)
class Company{
String region;
String code;
String type;
String contactPerson;
String startDate;
String endDate;
String field1;
String field2;
}
,第二个列表是List<Company>
(旧值)
如何比较两个列表并形成一个List<Map<string, Company>
,其中每个地图都有关键字&#34; old&#34;值为oldObj和key&#34; new&#34;值为newObj,其中要检查比较的字段是区域,代码,类型。
例如:
List<Company> companyList = Arrays.asList( new Company("1", "100", "tier1", "bob", "2010", "20201"), new Company("1", "101", "tier1", "rick", "2010", "20201"), new Company("1", "101", "tier2", "personA", "2010", "20201"), new Company("2", "200", "tier3", "personC", "2010", "20201"))
List<Company> dbValues = Arrays.asList( new Company("1", "100", "tier1", "jenny", "2010", "20201"), new Company("1", "101", "tier1", "rinson", "2010", "20201"), new Company("1", "101", "tier2", "personB", "2018", "2020"), new Company("2", "200", "tier3", "personD", "2010", "20201"))
感谢。
答案 0 :(得分:1)
您需要按步骤执行此操作:
Company
中的所有oldList
(也可以通过交换旧列表和新列表来完成)Company
:匹配code/type/region
"old" and "new"
为关键字的新地图中,然后在map
List<Company> oldList = // ;
List<Company> newList = // ;
List<Map<String, Company>> listMap = new ArrayList<>();
for (Company oldComp : oldList) {
newList.stream()
.filter(c -> c.code.equals(oldComp.code) &&
c.region.equals(oldComp.region) &&
c.type.equals(oldComp.type))
.findAny()
.ifPresent(newCorrespond -> {
Map<String, Company> map = new HashMap<>();
map.put("old", oldComp);
map.put("new", newCorrespond);
listMap.add(map);
});
}