From fb22934491c92ba052bc6d72fe57dcce97876e4b Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 27 Feb 2020 14:43:09 +0100 Subject: [PATCH] [New] Added keepInsertionOrder flag to MapOf* collections --- .../strolch/utils/collections/MapOfLists.java | 7 ++++ .../strolch/utils/collections/MapOfMaps.java | 41 ++++++++++++++++++- .../strolch/utils/collections/MapOfSets.java | 31 ++++++++++++-- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfLists.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfLists.java index edefadf0c..e803e6dea 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfLists.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfLists.java @@ -31,6 +31,13 @@ public class MapOfLists { this.mapOfLists = new HashMap<>(); } + public MapOfLists(boolean keepInsertionOrder) { + if (keepInsertionOrder) + this.mapOfLists = new LinkedHashMap<>(); + else + this.mapOfLists = new HashMap<>(); + } + public MapOfLists(Map> mapOfLists) { this.mapOfLists = mapOfLists; } diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfMaps.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfMaps.java index d931608e2..148c6bd5e 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfMaps.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfMaps.java @@ -45,20 +45,57 @@ import java.util.function.Function; */ public class MapOfMaps { + private final boolean keepInsertionOrder; private Map> mapOfMaps; public MapOfMaps() { + this.keepInsertionOrder = false; this.mapOfMaps = new HashMap<>(); } public MapOfMaps(Map> mapOfMaps) { + this.keepInsertionOrder = false; this.mapOfMaps = mapOfMaps; } + public MapOfMaps(Map> mapOfMaps, boolean keepInsertionOrder) { + this.mapOfMaps = mapOfMaps; + this.keepInsertionOrder = keepInsertionOrder; + } + public MapOfMaps(int initialSize) { + this.keepInsertionOrder = false; this.mapOfMaps = new HashMap<>(initialSize); } + public MapOfMaps(boolean keepInsertionOrder) { + this.keepInsertionOrder = keepInsertionOrder; + this.mapOfMaps = getMapOfMaps(); + } + + public MapOfMaps(int initialSize, boolean keepInsertionOrder) { + this.keepInsertionOrder = keepInsertionOrder; + this.mapOfMaps = getMapOfMaps(initialSize); + } + + private Map> getMapOfMaps() { + if (this.keepInsertionOrder) + return new LinkedHashMap<>(); + return new HashMap<>(); + } + + private Map> getMapOfMaps(int initialSize) { + if (this.keepInsertionOrder) + return new LinkedHashMap<>(initialSize); + return new HashMap<>(initialSize); + } + + private HashMap getMap() { + if (this.keepInsertionOrder) + return new LinkedHashMap<>(); + return new HashMap<>(); + } + public Set keySet() { return this.mapOfMaps.keySet(); } @@ -81,7 +118,7 @@ public class MapOfMaps { } public V addElement(T t, U u, V v) { - return this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()).put(u, v); + return this.mapOfMaps.computeIfAbsent(t, k -> getMap()).put(u, v); } public List getAllElements() { @@ -102,7 +139,7 @@ public class MapOfMaps { } public void addMap(T t, Map u) { - this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()).putAll(u); + this.mapOfMaps.computeIfAbsent(t, k -> getMap()).putAll(u); } public V removeElement(T t, U u) { diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java index 77cbbeb74..8273ef1af 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java @@ -25,16 +25,41 @@ import java.util.function.Function; */ public class MapOfSets { + private final boolean keepInsertionOrder; private Map> mapOfSets; public MapOfSets() { - this.mapOfSets = new HashMap<>(); + this.keepInsertionOrder = false; + this.mapOfSets = getMapOfSets(); } public MapOfSets(Map> mapOfSets) { + this.keepInsertionOrder = false; this.mapOfSets = mapOfSets; } + public MapOfSets(boolean keepInsertionOrder) { + this.keepInsertionOrder = keepInsertionOrder; + this.mapOfSets = getMapOfSets(); + } + + public MapOfSets(Map> mapOfSets, boolean keepInsertionOrder) { + this.keepInsertionOrder = keepInsertionOrder; + this.mapOfSets = mapOfSets; + } + + private HashMap> getMapOfSets() { + if (this.keepInsertionOrder) + return new LinkedHashMap<>(); + return new HashMap<>(); + } + + private HashSet getSet() { + if (this.keepInsertionOrder) + return new LinkedHashSet<>(); + return new HashSet<>(); + } + public Set keySet() { return this.mapOfSets.keySet(); } @@ -50,11 +75,11 @@ public class MapOfSets { } public boolean addElement(T t, U u) { - return this.mapOfSets.computeIfAbsent(t, k -> new HashSet<>()).add(u); + return this.mapOfSets.computeIfAbsent(t, k -> getSet()).add(u); } public boolean addSet(T t, Set u) { - return this.mapOfSets.computeIfAbsent(t, k -> new HashSet<>()).addAll(u); + return this.mapOfSets.computeIfAbsent(t, k -> getSet()).addAll(u); } public boolean removeElement(T t, U u) {