1.目的java
以前的項目中有個需求,要求不可使用多表聯查,分別查詢多張表的數據,再經過兩張表的關聯id將查詢出的兩個list數據合併成一個list 若是有兩個List,List< A>、List< B >, 其中,A的主鍵爲B的外鍵,如今要將他們合併成一個列表List<Map<A.id,B>>。bash
2.首先先導入guava依賴包app
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
複製代碼
3.demo代碼ide
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
public class ListUtil {
public static void main(String[] args) {
Aoo a = new Aoo(11, "a1", "123");
Aoo a2 = new Aoo(22, "a2", "12345");
Aoo a3 = new Aoo(23, "a2222", "1234522");
Aoo a4 = new Aoo(24, "a2", "12345");
Aoo a5 = new Aoo(25, "a2", "12345");
Boo b = new Boo(1, 11, "b1", "demo");
Boo b2 = new Boo(2, 22, "b2", "demo2");
Boo b3 = new Boo(3, 23, "b3", "demo3");
List<Aoo> aooList = Lists.newArrayList(a, a2, a3, a4, a5);
List<Boo> booList = Lists.newArrayList(b, b2, b3);
List<AooVO> result = Lists.newArrayList();
//先將booList轉成以aooId爲key,boo爲value的map
Map<Integer, Boo> booMap = Maps.uniqueIndex(booList, new Function<Boo, Integer>() {
@Override
public Integer apply(Boo boo) {
return boo.getAooId();
}
});
//經過aooId從booMap獲取boo對象,再拼接爲最後須要的VO對象
for (Aoo aoo : aooList) {
Boo boo = booMap.get(aoo.getId());
if (boo != null) {
AooVO aooVO = new AooVO();
aooVO.setId(aoo.getId());
aooVO.setName(aoo.getName());
aooVO.setValue(aoo.getValue());
aooVO.setType(boo.getType());
aooVO.setbName(boo.getName());
result.add(aooVO);
}
}
System.out.println(result);
//經過aooId排序
Collections.sort(result, new Comparator<AooVO>() {
@Override
public int compare(AooVO o1, AooVO o2) {
return o2.getId()-o1.getId();
}
});
System.out.println(result);
}
}
class Aoo {
private Integer id;
private String name;
private String value;
public Aoo(Integer id, String name, String value) {
this.id = id;
this.name = name;
this.value = value;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Aoo{" +
"id=" + id +
", name='" + name + '\'' + ", value='" + value + '\'' + '}'; } } class Boo { private Integer id; private Integer aooId; private String name; private String type; public Boo(Integer id, Integer aooId, String name, String type) { this.id = id; this.aooId = aooId; this.name = name; this.type = type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAooId() { return aooId; } public void setAooId(Integer aooId) { this.aooId = aooId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Boo{" + "id=" + id + ", aooId=" + aooId + ", name='" + name + '\'' +
", type='" + type + '\'' + '}'; } } class AooVO { private Integer id; private String name; private String bName; private String value; private String type; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getbName() { return bName; } public void setbName(String bName) { this.bName = bName; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "AooVO{" + "id=" + id + ", name='" + name + '\'' + ", bName='" + bName + '\'' +
", value='" + value + '\'' + ", type='" + type + '\'' + '}'; } } 複製代碼