Class MapCreate<K,V>
Object
MapCreate<K,V>
- Type Parameters:
K
- identifier (key)-typeV
- value-type
A tree map that creates a new item, if it doesn't already exist upon a get operation.
Internally it uses a HashMap
for it's implementation, and the K
and V
types must obey the rules for a HashMap
(with valid equals, hashcode etc.)
This structure is not inherently thread-safe.
- Author:
- Owen Feehan
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncomputeIfAbsent
(K key) Gets an existing element from the map, newly creating and storing the element if it's absent.entrySet()
The entries in the map.Gets an existing element from the map, returning null if it is absent.boolean
isEmpty()
Whether the map is empty or not.<E extends Exception>
voiditerateEntries
(CheckedBiConsumer<K, V, E> operation) Iterate over each entry in the map, and apply an operation.keySet()
The keys in the map.void
Removes the entry for the specified key only if it is currently mapped to the specified value.
-
Constructor Details
-
MapCreate
Creates without a comparator, using aHashMap
internally.- Parameters:
createNewElement
- called as necessary to create a new element in the tree.
-
MapCreate
Creates with an explicit comparator, and using aTreeMap
internally.- Parameters:
createNewElement
- called as necessary to create a new element in the tree.comparator
- used to impose an ordering on elements.
-
-
Method Details
-
get
Gets an existing element from the map, returning null if it is absent.- Parameters:
key
- the key for the map query.- Returns:
- an element, either retrieved from the map, or null, if none exists in the map.
-
computeIfAbsent
Gets an existing element from the map, newly creating and storing the element if it's absent.- Parameters:
key
- the key for the map query.- Returns:
- an element, either retrieved from the map, or newly created.
-
remove
Removes the entry for the specified key only if it is currently mapped to the specified value.- Parameters:
key
- the key to remove.value
- the value to remove.
-
entrySet
The entries in the map.- Returns:
- a set view of the entries contained in this map.
-
keySet
The keys in the map.- Returns:
- a set view of the keys contained in this map.
-
isEmpty
public boolean isEmpty()Whether the map is empty or not.- Returns:
- true iff the map is empty.
-
iterateEntries
Iterate over each entry in the map, and apply an operation.- Type Parameters:
E
- an exception that may be thrown by {code operation}.- Parameters:
operation
- the operation applied to each element in the map, passing the key and value as parameters.- Throws:
E
- ifoperation
throws it.
-