两个list对象根据里面的某一属性值合并

//两个list对象根据里面的某一属性合并
public static void main(String[] args) {
    List<Map<String,String>> mlist= new ArrayList<>();
    List<Map<String,String>> slist= new ArrayList<>();
    //实体对象同理
    Map<String,String> map = new HashMap<>();
    map.put("id","001");
    map.put("name","猴子");
    mlist.add(map);
    map = new HashMap<>();
    map.put("id","002");
    map.put("name","猪八戒");
    mlist.add(map);
    map = new HashMap<>();
    map.put("id","003");
    map.put("name","金禅");
    mlist.add(map);
    map = new HashMap<>();
    map.put("id","004");
    map.put("name","嫦娥");
    mlist.add(map);
    map = new HashMap<>();
    map.put("id","005");
    map.put("name","杨戬");
    mlist.add(map);
    map = new HashMap<>();
    map.put("id","001");
    map.put("type","打野");
    slist.add(map);
    map = new HashMap<>();
    map.put("id","002");
    map.put("type","战士");
    slist.add(map);
    map = new HashMap<>();
    map.put("id","003");
    map.put("type","辅助");
    slist.add(map);
    map = new HashMap<>();
    map.put("id","004");
    map.put("type","中单");
    slist.add(map);
    map = new HashMap<>();
    map.put("id","005");
    map.put("type","战士");
    slist.add(map);
    List<Map<String,String>> list = mlist.stream().map(ml->{
        slist.stream().filter(sl->Objects.equals(ml.get("id"),sl.get("id"))).forEach(sl->{
            ml.put("type",sl.get("type"));
        });
        return ml;
    }).collect(Collectors.toList());
    System.out.println(list);
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值