[New] Added keepInsertionOrder flag to MapOf* collections

This commit is contained in:
Robert von Burg 2020-02-27 14:43:09 +01:00
parent 3bf912c94b
commit fb22934491
3 changed files with 74 additions and 5 deletions

View File

@ -31,6 +31,13 @@ public class MapOfLists<T, U> {
this.mapOfLists = new HashMap<>();
}
public MapOfLists(boolean keepInsertionOrder) {
if (keepInsertionOrder)
this.mapOfLists = new LinkedHashMap<>();
else
this.mapOfLists = new HashMap<>();
}
public MapOfLists(Map<T, List<U>> mapOfLists) {
this.mapOfLists = mapOfLists;
}

View File

@ -45,20 +45,57 @@ import java.util.function.Function;
*/
public class MapOfMaps<T, U, V> {
private final boolean keepInsertionOrder;
private Map<T, Map<U, V>> mapOfMaps;
public MapOfMaps() {
this.keepInsertionOrder = false;
this.mapOfMaps = new HashMap<>();
}
public MapOfMaps(Map<T, Map<U, V>> mapOfMaps) {
this.keepInsertionOrder = false;
this.mapOfMaps = mapOfMaps;
}
public MapOfMaps(Map<T, Map<U, V>> 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<T, Map<U, V>> getMapOfMaps() {
if (this.keepInsertionOrder)
return new LinkedHashMap<>();
return new HashMap<>();
}
private Map<T, Map<U, V>> getMapOfMaps(int initialSize) {
if (this.keepInsertionOrder)
return new LinkedHashMap<>(initialSize);
return new HashMap<>(initialSize);
}
private HashMap<U, V> getMap() {
if (this.keepInsertionOrder)
return new LinkedHashMap<>();
return new HashMap<>();
}
public Set<T> keySet() {
return this.mapOfMaps.keySet();
}
@ -81,7 +118,7 @@ public class MapOfMaps<T, U, V> {
}
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<V> getAllElements() {
@ -102,7 +139,7 @@ public class MapOfMaps<T, U, V> {
}
public void addMap(T t, Map<U, V> 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) {

View File

@ -25,16 +25,41 @@ import java.util.function.Function;
*/
public class MapOfSets<T, U> {
private final boolean keepInsertionOrder;
private Map<T, Set<U>> mapOfSets;
public MapOfSets() {
this.mapOfSets = new HashMap<>();
this.keepInsertionOrder = false;
this.mapOfSets = getMapOfSets();
}
public MapOfSets(Map<T, Set<U>> mapOfSets) {
this.keepInsertionOrder = false;
this.mapOfSets = mapOfSets;
}
public MapOfSets(boolean keepInsertionOrder) {
this.keepInsertionOrder = keepInsertionOrder;
this.mapOfSets = getMapOfSets();
}
public MapOfSets(Map<T, Set<U>> mapOfSets, boolean keepInsertionOrder) {
this.keepInsertionOrder = keepInsertionOrder;
this.mapOfSets = mapOfSets;
}
private HashMap<T, Set<U>> getMapOfSets() {
if (this.keepInsertionOrder)
return new LinkedHashMap<>();
return new HashMap<>();
}
private HashSet<U> getSet() {
if (this.keepInsertionOrder)
return new LinkedHashSet<>();
return new HashSet<>();
}
public Set<T> keySet() {
return this.mapOfSets.keySet();
}
@ -50,11 +75,11 @@ public class MapOfSets<T, U> {
}
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> 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) {