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