测试代码如下:—-可以直接运行的
private static void xunhaun() {
List<Td51CustRisk> td51CustRiskList = new ArrayList<>();
Td51CustRisk td51CustRisk1 = new Td51CustRisk();
td51CustRisk1.setCustNo("123");
Td51CustRisk td51CustRisk2 = new Td51CustRisk();
td51CustRisk2.setCustNo("234");
Td51CustRisk td51CustRisk3 = new Td51CustRisk();
td51CustRisk3.setCustNo("345");
Td51CustRisk td51CustRisk4 = new Td51CustRisk();
td51CustRisk4.setCustNo("456");
td51CustRiskList.add(td51CustRisk1);
td51CustRiskList.add(td51CustRisk2);
td51CustRiskList.add(td51CustRisk3);
td51CustRiskList.add(td51CustRisk4);
List<Map<String, Object>> custInfoList = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("custNo", "345");
map1.put("custName", "345的名字");
map1.put("appOrg", "345的appOrg");
map1.put("custType", "345的custType");
Map<String, Object> map2 = new HashMap<>();
map2.put("custNo", "234");
map2.put("custName", "234的名字");
map2.put("appOrg", "234的appOrg");
map2.put("custType", "234的custType");
Map<String, Object> map3 = new HashMap<>();
map3.put("custNo", "123");
map3.put("custName", "123的名字");
map3.put("appOrg", "123的appOrg");
map3.put("custType", "123的custType");
Map<String, Object> map4 = new HashMap<>();
map4.put("custNo", "456");
map4.put("custName", "456的名字");
map4.put("appOrg", "456的appOrg");
map4.put("custType", "456的custType");
custInfoList.add(map1);
custInfoList.add(map2);
custInfoList.add(map3);
custInfoList.add(map4);
Map<String, Td51CustRisk> custRiskListMap = td51CustRiskList.stream().collect(
Collectors.toMap(w -> w.getCustNo(),
w -> w));
for (int i = 0; i < custInfoList.size(); i++) {
//匹配客户号
String custNo = custInfoList.get(i).get("custNo").toString();
Td51CustRisk td51CustRisk = custRiskListMap.get(custNo);
//客户所属机构
Object appOrg = custInfoList.get(i).get("appOrg");
td51CustRisk.setOrgKey(String.valueOf(appOrg));
//客户名称
Object custName = custInfoList.get(i).get("custName");
if (!ObjectUtils.isEmpty(custName)) {
td51CustRisk.setCustCnName(String.valueOf(custName));
}
//客户类型
Object custType = custInfoList.get(i).get("custType");
if (!ObjectUtils.isEmpty(custType)) {
td51CustRisk.setCustType(String.valueOf(custType));
}
System.out.println("custNo:" + td51CustRisk.getCustNo() + "--appOrg:" + td51CustRisk.getOrgKey() + "--custName:" + td51CustRisk.getCustCnName() + "---custType:" + td51CustRisk.getCustType());
}
}
实体类:
package dto;
public class Td51CustRisk {
private String custNo;
private String custCnName;
private String orgKey;
private String custType;
public String getCustNo() {
return custNo;
}
public void setCustNo(String custNo) {
this.custNo = custNo;
}
public String getCustCnName() {
return custCnName;
}
public void setCustCnName(String custCnName) {
this.custCnName = custCnName;
}
public String getOrgKey() {
return orgKey;
}
public void setOrgKey(String orgKey) {
this.orgKey = orgKey;
}
public String getCustType() {
return custType;
}
public void setCustType(String custType) {
this.custType = custType;
}
}
运行的结果:
解释:
将list通过java8特性 改成map的形式—-将custNo作为map的key值,保证唯一性。
Map
Collectors.toMap(w -> w.getCustNo(),
w -> w));
再通过for循环—-根据custNo做匹配即可