You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何将第二个列表的Map元素合并到第一个列表的每个Map中并生成目标集合?

Solution to Merge Map Elements from Second List into Each Map of First List

Hey there! I see what you're trying to do here—your current code is just concatenating the two lists instead of merging the contents of the second list's maps into each map in the first list. Let's fix that.

The Problem with Your Current Code

Your use of Stream.of(viewList, viewList1).flatMap(x -> x.stream()) simply takes all elements from both lists and puts them into a single list. That's why you end up with the extra {BASEQUANTITY=3} entry at the end instead of having that key-value pair in every map from viewList.

The Fix

We need to:

  1. Collect all key-value pairs from viewList1 into a single map (this handles cases where viewList1 has multiple maps too)
  2. Iterate over each map in viewList, create a copy of it, and merge the collected key-value pairs into the copy

Here's the updated code:

List<Map<String, Object>> viewList = new ArrayList<Map<String,Object>>();
List<Map<String, Object>> viewList1 = new ArrayList<Map<String,Object>>();
Map<String, Object> viewMap1 = new HashMap<String, Object>();
viewMap1.put("ARTICLE_ID", "000000001000100126");
viewMap1.put("SOURCE_ID", "0000007001");
viewMap1.put("UOM", "CS");
Map<String, Object> viewMap2 = new HashMap<String, Object>();
viewMap2.put("ARTICLE_ID", "000000001000100126");
viewMap2.put("SOURCE_ID", "0000007001");
viewMap2.put("UOM", "EA");
Map<String, Object> viewMap3 = new HashMap<String, Object>();
viewMap3.put("ARTICLE_ID", "000000001000100126");
viewMap3.put("SOURCE_ID", "0000007001");
viewMap3.put("UOM", "YD2");
Map<String, Object> viewMap4 = new HashMap<String, Object>();
viewMap4.put("BASEQUANTITY", "3");
viewList.add(viewMap1);
viewList.add(viewMap2);
viewList.add(viewMap3);
viewList1.add(viewMap4);

// Step 1: Combine all entries from viewList1 into a single map
Map<String, Object> mergeEntries = viewList1.stream()
    .flatMap(map -> map.entrySet().stream())
    .collect(Collectors.toMap(
        Map.Entry::getKey,
        Map.Entry::getValue,
        (existing, replacement) -> replacement // Handle duplicate keys by keeping the latest one
    ));

// Step 2: Merge these entries into each map from viewList
List<Map<String, Object>> combinedList = viewList.stream()
    .map(originalMap -> {
        // Create a new HashMap to avoid modifying the original maps
        Map<String, Object> mergedMap = new HashMap<>(originalMap);
        mergedMap.putAll(mergeEntries);
        return mergedMap;
    })
    .collect(Collectors.toList());

System.out.println(combinedList);

What This Does

  • Step 1: We take every entry from every map in viewList1 and combine them into one map. If there are duplicate keys in viewList1, the last occurrence will overwrite the previous ones (you can adjust the merge function if you need different behavior).
  • Step 2: For each map in viewList, we create a new copy (so we don't alter the original maps) and add all the entries from our combined mergeEntries map into it. This gives you exactly the output you're looking for.

Expected Output

[{UOM=CS, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126, BASEQUANTITY=3}, {UOM=EA, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126, BASEQUANTITY=3}, {UOM=YD2, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126, BASEQUANTITY=3}]

内容的提问来源于stack exchange,提问作者Vikram Aditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:02:50