@Test public void testMapMerge() { // given Map<String, Integer> mutableMap = new HashMap<>(Map.of("a", 1, "b", 11)); Map<String, Integer> otherMap = Map.of("b", 22, "c", 111); // when otherMap.forEach((k, v) -> mutableMap.merge(k, v, (v1, v2) -> v1 + v2)); // then Assert.assertEquals(mutableMap, Map.of("a", 1, "b", (11 + 22), "c", 111)); // when mutableMap.merge("b", 44, (v1, v2) -> v1 + v2); mutableMap.merge("d", 1111, (v1, v2) -> v1 + v2); // then Assert.assertEquals(mutableMap, Map.of("a", 1, "b", (11 + 22 + 44), "c", 111, "d", 1111)); }