From 7fb130ce392d27921ed2f585d351c36b6d4c786f Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 29 Aug 2018 13:12:07 +0200 Subject: [PATCH] [New] Allow to set backing map in MapOfLists, MapOfMaps or MapOfSets --- .../strolch/utils/collections/MapOfLists.java | 31 ++++++------------- .../strolch/utils/collections/MapOfMaps.java | 17 +++++----- .../strolch/utils/collections/MapOfSets.java | 30 ++++++------------ 3 files changed, 27 insertions(+), 51 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 8527fc4e2..3abf8084e 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 @@ -1,12 +1,12 @@ /* * Copyright 2013 Robert von Burg - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,13 +15,8 @@ */ package li.strolch.utils.collections; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; /** * @author Robert von Burg <eitch@eitchnet.ch> @@ -34,6 +29,10 @@ public class MapOfLists { this.mapOfLists = new HashMap<>(); } + public MapOfLists(Map> mapOfLists) { + this.mapOfLists = mapOfLists; + } + public Set keySet() { return this.mapOfLists.keySet(); } @@ -43,21 +42,11 @@ public class MapOfLists { } public boolean addElement(T t, U u) { - List list = this.mapOfLists.get(t); - if (list == null) { - list = new ArrayList<>(); - this.mapOfLists.put(t, list); - } - return list.add(u); + return this.mapOfLists.computeIfAbsent(t, k -> new ArrayList<>()).add(u); } public boolean addList(T t, List u) { - List list = this.mapOfLists.get(t); - if (list == null) { - list = new ArrayList<>(); - this.mapOfLists.put(t, list); - } - return list.addAll(u); + return this.mapOfLists.computeIfAbsent(t, k -> new ArrayList<>()).addAll(u); } public boolean removeElement(T t, U u) { 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 b32750292..93afbf35e 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 @@ -15,13 +15,8 @@ */ package li.strolch.utils.collections; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; /** *

@@ -54,6 +49,10 @@ public class MapOfMaps { this.mapOfMaps = new HashMap<>(); } + public MapOfMaps(Map> mapOfMaps) { + this.mapOfMaps = mapOfMaps; + } + public MapOfMaps(int initialSize) { this.mapOfMaps = new HashMap<>(initialSize); } @@ -74,8 +73,7 @@ public class MapOfMaps { } public V addElement(T t, U u, V v) { - Map map = this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()); - return map.put(u, v); + return this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()).put(u, v); } public List getAllElements() { @@ -96,8 +94,7 @@ public class MapOfMaps { } public void addMap(T t, Map u) { - Map map = this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()); - map.putAll(u); + this.mapOfMaps.computeIfAbsent(t, k -> new HashMap<>()).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 fd820abff..bda7f39df 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 @@ -1,12 +1,12 @@ /* * Copyright 2016 Robert von Burg - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,12 +15,8 @@ */ package li.strolch.utils.collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; /** * @author Robert von Burg <eitch@eitchnet.ch> @@ -33,6 +29,10 @@ public class MapOfSets { this.mapOfSets = new HashMap<>(); } + public MapOfSets(Map> mapOfSets) { + this.mapOfSets = mapOfSets; + } + public Set keySet() { return this.mapOfSets.keySet(); } @@ -42,21 +42,11 @@ public class MapOfSets { } public boolean addElement(T t, U u) { - Set set = this.mapOfSets.get(t); - if (set == null) { - set = new HashSet<>(); - this.mapOfSets.put(t, set); - } - return set.add(u); + return this.mapOfSets.computeIfAbsent(t, k -> new HashSet<>()).add(u); } public boolean addSet(T t, Set u) { - Set set = this.mapOfSets.get(t); - if (set == null) { - set = new HashSet<>(); - this.mapOfSets.put(t, set); - } - return set.addAll(u); + return this.mapOfSets.computeIfAbsent(t, k -> new HashSet<>()).addAll(u); } public boolean removeElement(T t, U u) {