天天看点

通过Guava实现两个包含不同对象的List合并成一个List

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 + '\'' +
                '}';
    }
}