【问题标题】:Is there a way to combine two list which will have second list element into each and every sublist in first one?有没有办法将两个列表合并到第一个列表中的每个子列表中?
【发布时间】:2021-10-12 20:35:29
【问题描述】:

这是我的代码,它将返回组合列表:

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);

List<Map<String, Object>> combinedList = Stream.of(viewList, viewList1)
                                               .flatMap(x -> x.stream())
                                               .collect(Collectors.toList());
System.out.println(combinedList);

我得到如下输出:

[{uom = cs,source_id = 0000001000100126},{uom = ea,source_id = 0000007001,stition_id = 000000001000100126},{uom = yd2,source_id = 000000007001,Starts_id = 000000001000100126},{basequantive = 3 }]

我想要这样的输出:

[{UOM=CS, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126, BASEQUANTITY=3}, {UOM=EA, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126, BASEQUANTITY=3}, {UOM_00ID00702, ARTICLE_ID=YDCE=02, =000000001000100126,基量=3}]

如何将最后一个元素,即combinedList 的 BASEQUANTITY=3 添加到第一个 List 的每个子列表中,并从 combinedList 中删除该元素,如上面的输出所示。请帮忙。

【问题讨论】:

  • 为什么列表中有第二张地图?似乎您想要的只是将一个条目从viewList 复制到所有列表中?
  • @ernest_k , 我们需要合并 2 个地图列表。
  • 你想要这个吗? List> combinedList = viewList.stream().map(x -> x.putAll(viewMap4)).collect(Collectors.toList());
  • @Preston,我不确定它是否会给我一个错误。你能验证你的答案吗?
  • 更新:List> combinedList = viewList.stream().map(x -> { x.putAll(viewMap4); return x; }).collect(Collectors. toList());

标签: java arraylist collections java-8 java-stream


【解决方案1】:

不要将它们一起流式传输,因为看起来您需要将第二个列表中每个映射的每个键值对添加到第一个列表中的每个映射。

// first add everything from viewList to combinedList
List<Map<String, Object>> combinedList = new ArrayList<>(viewList);

// Now for every map in combinedList (which is now equivalent to viewList),
// add contents of every map from viewList1 to combinedList.
combinedList.forEach(map -> viewList1.forEach(map2 -> map.putAll(map2)));

or,
```java
combinedList.forEach(map -> viewList1.forEach(map::putAll));

编辑: 尽管combinedList 中元素的添加/删除不会反映在viewList 中,正如Alex 指出的那样,combinedList 中映射元素的任何更改都将反映在viewList 的映射中(因为new ArrayList&lt;&gt;(viewList) 用于创建combinedList)。

为了避免这种情况,combinedList 可以初始化为:

List<Map<String, Object>> combinedList = viewList.stream().map(HashMap::new).collect(Collectors.toList());

【讨论】:

  • 能否请您简要介绍一下您的答案我想要的combinedList的结果如List>
  • @VikramAditya 我已经更新了答案。希望现在很清楚
【解决方案2】:

The solution offered by @Gautham M 影响viewList - 因为combinedList 是作为viewList 的浅表副本创建的,并且两个列表引用相同的映射。因此,当将viewList1 的内容添加到combinedList 中的每个映射时,它也会“出现”在viewList 中。

另外,一般情况下,如果需要添加viewList1中所有地图的所有条目的内容,则应将此列表展平为单个地图,然后添加此小计地图到viewList中的地图副本:

Map<String, Object> subtotal = viewList1
    .stream()
    .flatMap(map -> map.entrySet().stream())
    .collect(Collectors.toMap(
        Map.Entry::getKey, 
        Map.Entry::getValue, 
        (v1, v2) -> v1 // resolve possible conflicts by keeping the first value
    ));

List<Map<String, Object>> combinedList = viewList
    .stream() // Stream<Map>
    .map(HashMap::new) // copy map is created
    .peek(copy -> copy.putAll(subtotal))
    .collect(Collectors.toList());

System.out.println("combined:\n" + combinedList);

System.out.println("\ninitial:\n" + viewList);

输出

综合:
[{BASEQUANTITY=3, UOM=CS, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126}, {BASEQUANTITY=3, UOM=EA, SOURCE_ID=0000007001, ARTICLE_ID=000000001000100126}, {BASEQUANTITY=3, UOM=0000002 ARTICLE_ID=000000001000100126}]

初始:
[{UOM = CS,source_id = 0000001000100126},{uom = ea,source_id = 0000007001,{uom = yd2,source_id = 0000007001,Starts_id = 000000001000100126}]

【讨论】:

    猜你喜欢
    • 2013-12-14
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2020-08-04
    • 2020-09-27
    • 2021-11-12
    • 2018-11-23
    相关资源
    最近更新 更多