I have two (or more) Map<String, Integer>
objects. I'd like to merge them with Java 8 Stream API in a way that values for common keys should be the maximum of the values.
@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);
}
How can I make this test method green?
I've played with collect
and Collectors
for a while without any success.
(ImmutableMap
and newArrayList
are from Google Guava.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…