1.目的
之前的專案中有個需求,要求不可以使用多表聯查,分別查詢多張表的資料,再通過兩張表的關聯id將查詢出的兩個list資料合併成一個list 如果有兩個List,List< A>、List< B >, 其中,A的主鍵為B的外來鍵,現在要將他們合併成一個列表List<Map<A.id,B>>。
2.首先先匯入guava依賴包
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
複製程式碼
3.demo程式碼
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 + '\'' +
'}';
}
}
複製程式碼