Java 如何根据第一个列表中的字段将第二个列表与第一个列表合并?

Java 如何根据第一个列表中的字段将第二个列表与第一个列表合并?,java,arraylist,java-8,Java,Arraylist,Java 8,我有两个列表,第一个列表包含一个可以复制的字段,第二个列表包含同一个字段,不与其他字段重复。我需要的是根据公共字段将两个列表合并为一个列表 清单1: [ { "id" : "1" "name : "hello", "item_name": "Name" }, { "id" : "1" "name : "hi" "item_name": "Name2" }, { "id" : "2" "name : "hello" "it

我有两个列表,第一个列表包含一个可以复制的字段,第二个列表包含同一个字段,不与其他字段重复。我需要的是根据公共字段将两个列表合并为一个列表

清单1:

[
{
    "id" : "1"
    "name : "hello",
    "item_name": "Name"

},
{
    "id" : "1"
    "name : "hi"
    "item_name": "Name2"

},
{
    "id" : "2"
    "name : "hello"
    "item_name": "Name"

}
]
第二份名单:

[{
    "id" : "1"
    "age" :  "10",
    "place" : "0",
    "date" : "0"
},
{
    "id" : "2"
    "age" :  "12",
    "place" : "1",
    "date" : "0
}]
预期结果:

[
    {
            "id" : "1"
            "name : "hello",
            "item_name": "Name",
            **"age" :  "10",
            "place" : "0",
            "date" : "0**
    },
    {
            "id" : "1"
            "name : "hi"
            "item_name": "Name2"
            **"age" :  "10",
            "place" : "0",
            "date" : "0"**
    },
    {
            "id" : "2"
            "name : "hello"
            "item_name": "Name"
            **"age" :  "12",
            "place" : "1",
            "date" : "0"**
    }
    ]
粗体区域从第二个列表中加入 请帮忙做这件事

我试过:

Map<String, String> listMap = list2
            .stream()
            .collect(Collectors.toMap(List1Entity::getId,
                    List1Entity::getId));
    list1.forEach(a -> {
        a.setPlace(listMap.get(a.getId()));
    });
Map listMap=list2
.stream()
.collect(Collectors.toMap(List1Entity::getId、,
List1Entity::getId));
清单1.forEach(a->{
a、 setPlace(listMap.get(a.getId());
});
但在.collect()处获取错误

不存在实例或类型变量,因此List2Entity符合 清单1实体


你是指第一句话中的
List2Entity

Map<String, String> listMap = list2
        .stream()
        .collect(Collectors.toMap(List2Entity::getId,
                List2Entity::getPlace));
Map listMap=list2
.stream()
.collect(Collectors.toMap)(List2Entity::getId,
List2Entity::getPlace));

在定义两个列表之间的连接部分时,您的要求有点不清楚

您可以从这样的内容开始,根据需要调整
mapFunction

String list1 = "[{\"id\":\"1\",\"name\":\"hello\",\"item_name\":\"Name\"},{\"id\":\"1\",\"name\":\"hi\",\"item_name\":\"Name2\"},{\"id\":\"2\",\"name\":\"hello\",\"item_name\":\"Name\"}]";
String list2 = "[{\"id\":\"1\",\"age\":\"10\",\"place\":\"0\",\"date\":\"0\"},{\"id\":\"2\",\"age\":\"12\",\"place\":\"1\",\"date\":\"0\"}]";

ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> l1 = mapper.readValue(list1, new TypeReference<List<Map<String, Object>>>(){});
List<Map<String, Object>> l2 = mapper.readValue(list2, new TypeReference<List<Map<String, Object>>>(){});

final UnaryOperator<Map<String, Object>> mapFunction = map -> {
    final String id = map.get("id").toString();
    l2.stream()
        .filter(map2 -> map2.get("id").toString().equals(id))
        .findAny()
        .ifPresent(map::putAll);
    return map;
};

List<Map<String, Object>> result = l1
        .stream()
        .map(mapFunction)
        .collect(Collectors.toList());
String list1=“[{\'id\':\'1\',\'name\':\'hello\',\'item\'u name\':\'name\',{\'id\':\'1\',\'name\':\'hi\',\'item\'u name\':\'Name2\',{\'id\':'2\',\'name\'name\':\'hello\',\'item\'u name\';
字符串列表2=“[{\'id\”:“1\”,“age\”:“10\”,“place\”:“0\”,“date\”:“0\”,{\'id\”:“2\”,“age\”:“12\”,“place\”:“1\”,“date\”:“0\”);
ObjectMapper mapper=新的ObjectMapper();
列表l1=mapper.readValue(列表1,新类型引用(){});
List l2=mapper.readValue(list2,new TypeReference(){});
最终一元运算符mapFunction=map->{
最终字符串id=map.get(“id”).toString();
l2.stream()
.filter(map2->map2.get(“id”).toString().equals(id))
.findAny()
.ifPresent(map::putAll);
返回图;
};
列表结果=l1
.stream()
.map(映射函数)
.collect(Collectors.toList());

从某种意义上讲,1听起来像是一个
列表
,2是一个
集合
。如果这能改善你正在尝试的东西。一定要分享问题中让你陷入困境的东西和实体。@Naman,不,两者都是对象列表。连接部分如问题中所述,即第一个列表不包含某些字段,而第二个列表包含这些字段,请将第二个列表中的参数连接到第一个列表中,以获得共同的ID。我希望你能明白