【问题标题】:How to join 2nd list with first list based on a field in 1st list?如何根据第一个列表中的字段将第二个列表与第一个列表连接起来?
【发布时间】:2019-07-24 06:52:28
【问题描述】:

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

清单 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()));
    });

但在 .collect() 处出错,

不存在实例或类型变量,因此 List2Entity 符合 List1Entity

【问题讨论】:

  • 在某种意义上听起来像 1 是 List 而 2 是 Set。如果这可以改善你正在尝试的东西。请分享让您陷入困境的原因以及问题中的实体。
  • @Naman,两者都不是对象列表

标签: java arraylist java-8


【解决方案1】:

您是指第一个语句中的List2Entity 吗?

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

【讨论】:

    【解决方案2】:

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

    您可以从这样的事情开始,然后根据您的需要调整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());
    

    【讨论】:

    • 加入部分如问题中所述,即第一个列表不包含某些字段,而第二个列表具有这些字段,将第二个列表中的参数连接到第一个列表中的公共ID .我希望你能明白
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多