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

  • Post author:
  • Post category:其他


//两个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);
}



版权声明:本文为weixin_53083415原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。