Package com.ontology2.pidove.util
Class DefaultMap<K,V>
java.lang.Object
com.ontology2.pidove.util.DefaultMap<K,V>
- Type Parameters:
K
- key data typeV
- value data type
- All Implemented Interfaces:
Map<K,
V>
Like the defaultdict in Python. If you get() a key that doesn't have a value, it sets() a value for it.
Among other things, the DefaultMap is a solution to the multimap problem, thatis, you can write
var m = new DefaultMap(ArrayList::new);
m.get("one").add(7)
m.get("one").add(8)
m.get("one") // equals List.of(7,8)
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionDefaultMap
(Supplier<V> defaultValue) Creates a new DefaultMap backed by a new HashMap -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) It's not so clear what this function should return, since even if a key is not contained in the inner map it would be if you got it.boolean
containsValue
(Object value) entrySet()
boolean
isEmpty()
keySet()
void
int
size()
values()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
innerMap
-
defaultValue
-
-
Constructor Details
-
DefaultMap
- Parameters:
innerMap
- the map contained insidedefaultValue
- a supplier that generates default values for the map; this has to be a Supplier because if you want this to be a container like a List you need a new one each time.
-
DefaultMap
Creates a new DefaultMap backed by a new HashMap- Parameters:
defaultValue
- a supplier that generates default values for the map; this has to be a Supplier because if you want this to be a container like a List you need a new one each time.
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
It's not so clear what this function should return, since even if a key is not contained in the inner map it would be if you got it. That kind of thinking would mean isEmpty() is always false and size() undefined in most cases. If this bothers you than try a different Multimap implementation.- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- key whose presence in this map is to be tested- Returns:
- the same thing the inner map would return
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-