[New] Allow to set backing map in MapOfLists, MapOfMaps or MapOfSets

This commit is contained in:
Robert von Burg 2018-08-29 13:12:07 +02:00
parent 458706d8fc
commit 7fb130ce39
3 changed files with 27 additions and 51 deletions

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* 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 &lt;eitch@eitchnet.ch&gt;
@ -34,6 +29,10 @@ public class MapOfLists<T, U> {
this.mapOfLists = new HashMap<>();
}
public MapOfLists(Map<T, List<U>> mapOfLists) {
this.mapOfLists = mapOfLists;
}
public Set<T> keySet() {
return this.mapOfLists.keySet();
}
@ -43,21 +42,11 @@ public class MapOfLists<T, U> {
}
public boolean addElement(T t, U u) {
List<U> 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> u) {
List<U> 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) {

View File

@ -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;
/**
* <p>
@ -54,6 +49,10 @@ public class MapOfMaps<T, U, V> {
this.mapOfMaps = new HashMap<>();
}
public MapOfMaps(Map<T, Map<U, V>> mapOfMaps) {
this.mapOfMaps = mapOfMaps;
}
public MapOfMaps(int initialSize) {
this.mapOfMaps = new HashMap<>(initialSize);
}
@ -74,8 +73,7 @@ public class MapOfMaps<T, U, V> {
}
public V addElement(T t, U u, V v) {
Map<U, V> 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<V> getAllElements() {
@ -96,8 +94,7 @@ public class MapOfMaps<T, U, V> {
}
public void addMap(T t, Map<U, V> u) {
Map<U, V> 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) {

View File

@ -1,12 +1,12 @@
/*
* Copyright 2016 Robert von Burg <eitch@eitchnet.ch>
*
*
* 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 &lt;eitch@eitchnet.ch&gt;
@ -33,6 +29,10 @@ public class MapOfSets<T, U> {
this.mapOfSets = new HashMap<>();
}
public MapOfSets(Map<T, Set<U>> mapOfSets) {
this.mapOfSets = mapOfSets;
}
public Set<T> keySet() {
return this.mapOfSets.keySet();
}
@ -42,21 +42,11 @@ public class MapOfSets<T, U> {
}
public boolean addElement(T t, U u) {
Set<U> 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> u) {
Set<U> 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) {