guva java list取交集_通过Guava实现两个包含不同对象的List合并成一个List

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

'}';

}

}复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值