diff --git a/src/main/java/org/organicdesign/fp/collections/PersistentHashMap.java b/src/main/java/org/organicdesign/fp/collections/PersistentHashMap.java index 9f04247..1c3a29c 100644 --- a/src/main/java/org/organicdesign/fp/collections/PersistentHashMap.java +++ b/src/main/java/org/organicdesign/fp/collections/PersistentHashMap.java @@ -156,7 +156,7 @@ private static int mask(int hash, int shift){ @SuppressWarnings("WeakerAccess") public static @NotNull PersistentHashMap ofEq( Equator eq, - @Nullable Iterable> es) { + @Nullable Iterable> es) { if (es == null) { return empty(eq); } MutHashMap map = emptyMutable(eq); for (Map.Entry entry : es) { @@ -179,7 +179,7 @@ private static int mask(int hash, int shift){ @return a new PersistentHashMap of the given key/value pairs */ - public static @NotNull PersistentHashMap of(@Nullable Iterable> kvPairs) { + public static @NotNull PersistentHashMap of(@Nullable Iterable> kvPairs) { if (kvPairs == null) { return empty(); } PersistentHashMap m = empty(); MutHashMap map = m.mutable(); diff --git a/src/main/java/org/organicdesign/fp/collections/PersistentTreeMap.java b/src/main/java/org/organicdesign/fp/collections/PersistentTreeMap.java index 1aa4a83..13dfda5 100644 --- a/src/main/java/org/organicdesign/fp/collections/PersistentTreeMap.java +++ b/src/main/java/org/organicdesign/fp/collections/PersistentTreeMap.java @@ -61,7 +61,7 @@ static class Box { any null Entries. */ public static ,V> PersistentTreeMap - of(Iterable> es) { + of(Iterable> es) { if (es == null) { return empty(); } PersistentTreeMap map = new PersistentTreeMap<>(Equator.defaultComparator(), null, 0); for (Map.Entry entry : es) { @@ -87,7 +87,7 @@ one null key (if your comparator knows how to sort nulls) and any number of null @return a new PersistentTreeMap of the specified comparator and the given key/value pairs */ public static PersistentTreeMap - ofComp(Comparator comp, Iterable> kvPairs) { + ofComp(Comparator comp, Iterable> kvPairs) { if (kvPairs == null) { return new PersistentTreeMap<>(comp, null, 0); } PersistentTreeMap map = new PersistentTreeMap<>(comp, null, 0); for (Map.Entry entry : kvPairs) { @@ -104,7 +104,7 @@ items that implement Comparable (have a "natural ordering"). An attempt to use items will blow up at runtime. Either a withComparator() method will be added, or this will be removed. */ - final static public PersistentTreeMap EMPTY = + final static public PersistentTreeMap EMPTY = new PersistentTreeMap<>(Equator.defaultComparator(), null, 0); /** diff --git a/src/test/java/org/organicdesign/fp/collections/PersistentHashMapTest.java b/src/test/java/org/organicdesign/fp/collections/PersistentHashMapTest.java index a0b26c1..65e60d9 100644 --- a/src/test/java/org/organicdesign/fp/collections/PersistentHashMapTest.java +++ b/src/test/java/org/organicdesign/fp/collections/PersistentHashMapTest.java @@ -24,7 +24,6 @@ import org.organicdesign.fp.oneOf.Option; import org.organicdesign.fp.tuple.Tuple2; -import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.*; import static org.organicdesign.fp.FunctionUtils.ordinal; import static org.organicdesign.fp.StaticImports.*; @@ -1618,4 +1617,26 @@ null, tup(3, "three"), null, tup(4, "four"), null) assertNotEquals(h2, h2.assoc(null, "nada")); assertEquals(h2.size() + 1, h2.assoc(null, "nada").size()); } + + @Test + public void testOfIterable() { + PersistentHashMap map = PersistentHashMap.empty() + .assoc("a", "a") + .assoc("b", "b") + .assoc("c", "c") + .assoc("d", "d"); + PersistentHashMap map2 = PersistentHashMap.empty() + .assoc("e", "e") + .assoc("f", "f") + .assoc("g", "g") + .assoc("h", "h"); + + assertEquals(map.without("a").without("b"), PersistentHashMap.of( + map.filter(entry -> !(entry.getKey().equals("a") || entry.getKey().equals("b"))))); + + ImMap combined = map.concat(map2).toImMap(x -> x); + assertEquals(combined, PersistentTreeMap.of(map.concat(map2))); + + assertEquals(map, PersistentTreeMap.of(map)); + } } diff --git a/src/test/java/org/organicdesign/fp/collections/PersistentTreeMapTest.java b/src/test/java/org/organicdesign/fp/collections/PersistentTreeMapTest.java index a958283..71476e9 100644 --- a/src/test/java/org/organicdesign/fp/collections/PersistentTreeMapTest.java +++ b/src/test/java/org/organicdesign/fp/collections/PersistentTreeMapTest.java @@ -925,4 +925,24 @@ public void subMapEx() { Fn1.identity()), max); } + + @Test public void testOfIterable() { + PersistentTreeMap map = PersistentTreeMap.empty() + .assoc("a", "a") + .assoc("b", "b") + .assoc("c", "c") + .assoc("d", "d"); + PersistentTreeMap map2 = PersistentTreeMap.empty() + .assoc("e", "e") + .assoc("f", "f") + .assoc("g", "g") + .assoc("h", "h"); + + assertEquals(map.without("a").without("b"), PersistentTreeMap.of(map.drop(2))); + + ImSortedMap combined = map.concat(map2).toImSortedMap(Equator.defaultComparator(), entry -> entry); + assertEquals(combined, PersistentTreeMap.of(map.concat(map2))); + + assertEquals(map, PersistentTreeMap.of(map)); + } }