侃侃尔雅
不知道你说的是不是这个:
package com.segmentfault.qa.java;
import java.util.*;
public class ListMergeTest {
public static void main(String[] args) {
List listA = new ArrayList<>();
List listB = new ArrayList<>();
Map> resultMap = new HashMap<>();
listA.add(new ClsA("001", "Alex"));
listA.add(new ClsA("002", "Bill"));
listA.add(new ClsA("003", "Carl"));
listB.add(new ClsB("001", 1000));
listB.add(new ClsB("002", 2000));
listB.add(new ClsB("004", 4000));
for(ClsA a: listA){
Map map = new HashMap<>();
String empNo = a.getEmpNo();
for(ClsB b: listB){
if(b.getEmpNo().equals(empNo)){
map.put("money", b.getMoney());
}
}
map.put("name", a.getName());
resultMap.put(empNo, map);
}
System.out.println(resultMap);
}
}
class ClsA{
private String empNo;
private String name;
public ClsA(String empNo, String name){
this.empNo = empNo;
this.name = name;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class ClsB{
private String empNo;
private int money;
public ClsB(String empNo, int money){
this.empNo = empNo;
this.money = money;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}