From fdf4d0b8a96f3dfc579c28e363be7944033f53a7 Mon Sep 17 00:00:00 2001 From: Bart Jacobs Date: Mon, 28 Jun 2021 21:41:30 +0200 Subject: [PATCH] LogicalMap: add methods `plus` and `minus` --- .../src/logicalcollections/LogicalMap.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/logicalcollections/src/logicalcollections/LogicalMap.java b/logicalcollections/src/logicalcollections/LogicalMap.java index dd070f8..5e19998 100644 --- a/logicalcollections/src/logicalcollections/LogicalMap.java +++ b/logicalcollections/src/logicalcollections/LogicalMap.java @@ -113,4 +113,24 @@ public static boolean allEntriesMatch(Map map, BiPredicate pr return map.keySet().stream().allMatch(k -> predicate.test(k, map.get(k))); } + /** + * Returns a map that contains the entries of the given map + * as well as the given entry. + */ + public static Map plus(Map map, K key, V value) { + var result = new HashMap<>(map); + result.put(key, value); + return result; + } + + /** + * Returns a map that contains the entries of the given map + * except for the entry with the given key. + */ + public static Map minus(Map map, K key) { + var result = new HashMap<>(map); + result.remove(key); + return result; + } + }