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 aooList = Lists.newArrayList(a, a2, a3, a4, a5);
List booList = Lists.newArrayList(b, b2, b3);
List result = Lists.newArrayList();
//先将booList转成以aooId为key,boo为value的map
Map booMap = Maps.uniqueIndex(booList, new Function() {
@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() {
@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 + '\'' +
'}';
}
}复制代码