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:
- Collect all key-value pairs from
viewList1into a single map (this handles cases whereviewList1has multiple maps too) - 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
viewList1and combine them into one map. If there are duplicate keys inviewList1, 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 combinedmergeEntriesmap 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
相关产品推荐
相关产品推荐

