当前位置: 首页 > 知识库问答 >
问题:

用Java8流API合并两个Map

宇文迪
2023-03-14

我有两个(或更多)Map

@Test
public void test14() throws Exception {
    Map<String, Integer> m1 = ImmutableMap.of("a", 2, "b", 3);
    Map<String, Integer> m2 = ImmutableMap.of("a", 3, "c", 4);
    List<Map<String, Integer>> list = newArrayList(m1, m2);

    Map<String, Integer> mx = list.stream()... // TODO

    Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 3, "c", 4);
    assertEquals(expected, mx);
}

我怎样才能使这个测试方法变成绿色?

我玩了一段时间的收集收集,但没有任何成功。

ImmutableMapnewArrayList来自谷歌番石榴。)


共有3个答案

漆雕深
2023-03-14
mx = list.stream().collect(HashMap::new,
        (a, b) -> b.forEach((k, v) -> a.merge(k, v, Integer::max)),
        Map::putAll);

这涵盖了任何大小列表的一般情况,应该适用于任何类型,只需根据需要交换Integer::max和/或HashMap::new

如果你不在乎合并中出现哪个值,有一个更干净的解决方案:

mx = list.stream().collect(HashMap::new, Map::putAll, Map::putAll);

作为一般方法:

public static <K, V> Map<K, V> mergeMaps(Stream<? extends Map<K, V>> stream) {
    return stream.collect(HashMap::new, Map::putAll, Map::putAll);
}

public static <K, V, M extends Map<K, V>> M mergeMaps(Stream<? extends Map<K, V>> stream,
        BinaryOperator<V> mergeFunction, Supplier<M> mapSupplier) {
    return stream.collect(mapSupplier,
            (a, b) -> b.forEach((k, v) -> a.merge(k, v, mergeFunction)),
            Map::putAll);
}
乐正心思
2023-03-14
Map<String, Integer> mx = new HashMap<>(m1);
m2.forEach((k, v) -> mx.merge(k, v, Integer::max));
罗智志
2023-03-14
@Test
public void test14() throws Exception {
    Map<String, Integer> m1 = ImmutableMap.of("a", 2, "b", 3);
    Map<String, Integer> m2 = ImmutableMap.of("a", 3, "c", 4);

    Map<String, Integer> mx = Stream.of(m1, m2)
        .map(Map::entrySet)          // converts each map into an entry set
        .flatMap(Collection::stream) // converts each set into an entry stream, then
                                     // "concatenates" it in place of the original set
        .collect(
            Collectors.toMap(        // collects into a map
                Map.Entry::getKey,   // where each entry is based
                Map.Entry::getValue, // on the entries in the stream
                Integer::max         // such that if a value already exist for
                                     // a given key, the max of the old
                                     // and new value is taken
            )
        )
    ;

    /* Use the following if you want to create the map with parallel streams
    Map<String, Integer> mx = Stream.of(m1, m2)
        .parallel()
        .map(Map::entrySet)          // converts each map into an entry set
        .flatMap(Collection::stream) // converts each set into an entry stream, then
                                     // "concatenates" it in place of the original set
        .collect(
            Collectors.toConcurrentMap(        // collects into a map
                Map.Entry::getKey,   // where each entry is based
                Map.Entry::getValue, // on the entries in the stream
                Integer::max         // such that if a value already exist for
                                     // a given key, the max of the old
                                     // and new value is taken
            )
        )
    ;
    */

    Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 3, "c", 4);
    assertEquals(expected, mx);
}
 类似资料:
  • 有没有办法把下面的2个状态结合起来呢? 我有一个类如下所示: 包含一个介于之间的随机值。 是一个包含大量

  • 我有一个Java类,它有20个属性及其相应的getter和setter。还有两个对象列表:和。 使用Java 8: 但是我必须在哪个地方指定属性呢?是否应该重写和方法?

  • 我试图使用

  • 我有以下情况 我必须合并所有列表从映射的对象。 知道如何使用Java8流API吗?

  • 问题内容: 我有两个(或更多)对象。我想将它们与Java 8 Stream API合并,以使通用密钥的值应为最大值。 如何使该测试方法绿色? 我已经打了,并有一阵子没有任何成功。 (并且来自Google Guava。) 问题答案:

  • 我想将两个ObjectNode合并在一起,但要具有最佳的复杂性。我知道一种方法,我可以使用setAll方法,但它返回JsonNode,因此我必须转换它。我发现的转换过程的最佳方法是如何将JsonNode转换为ObjectNode。我认为这种转换一旦迭代Json,也许我们可以找到一个更好的解决方案。 我认为第二种方法是迭代第二个ObjectNode,然后用put方法一个接一个地添加到第一个Objec