Package com.cedarsoftware.util
Class TrackingMap<K,V>
java.lang.Object
com.cedarsoftware.util.TrackingMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Map<K,V>
A wrapper around a
Map that tracks which keys have been accessed via get or containsKey methods.
This is useful for scenarios where it's necessary to monitor usage patterns of keys in a map,
such as identifying unused entries or optimizing memory usage by expunging rarely accessed keys.
Usage Example:
Map<String, Integer> originalMap = new HashMap<>();
originalMap.put("apple", 1);
originalMap.put("banana", 2);
originalMap.put("cherry", 3);
TrackingMap<String, Integer> trackingMap = new TrackingMap<>(originalMap);
// Access some keys
trackingMap.get("apple");
trackingMap.containsKey("banana");
// Expunge unused keys
trackingMap.expungeUnused();
// Now, "cherry" has been removed as it was not accessed
System.out.println(trackingMap.keySet()); // Outputs: [apple, banana]
Thread Safety: This class is not thread-safe. If multiple threads access a TrackingMap
concurrently and at least one of the threads modifies the map structurally, it must be synchronized externally.
Note: The expungeUnused() method removes all entries that have not been accessed via
get(Object) or containsKey(Object) since the map was created or since the last call to
expungeUnused().
- Author:
- Sean Kellner
Copyright (c) Cedar Software LLC
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
License
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. See the License for the specific language governing permissions and limitations under the License.
-
Nested Class Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all the mappings from this map.booleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.entrySet()booleanCompares the specified object with this map for equality.voidRemove the entries from the Map that have not been accessed by .get() or .containsKey().Retrieves the value associated with the specified key and marks the key as accessed.Returns the underlyingMapthat thisTrackingMapwraps.inthashCode()voidinformAdditionalUsage(TrackingMap<K, V> additional) Add the used keys from the passed in TrackingMap to this TrackingMap's keysUsed.voidinformAdditionalUsage(Collection<K> additional) Adds the accessed keys from anotherTrackingMapto this map's set of accessed keys.booleanisEmpty()keySet()keysUsed()Associates the specified value with the specified key in this map.voidCopies all the mappings from the specified map to this map.Removes the mapping for a key from this map if it is present.intsize()toString()values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
TrackingMap
Wraps the providedMapwith aTrackingMap.- Parameters:
map- theMapto be wrapped and tracked- Throws:
IllegalArgumentException- if the providedmapisnull
-
-
Method Details
-
get
Retrieves the value associated with the specified key and marks the key as accessed. -
put
Associates the specified value with the specified key in this map. -
containsKey
Returnstrueif this map contains a mapping for the specified key. Marks the key as accessed.- Specified by:
containsKeyin interfaceMap<K,V> - Parameters:
key- key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key
-
putAll
Copies all the mappings from the specified map to this map.- Specified by:
putAllin interfaceMap<K,V> - Parameters:
m- mappings to be stored in this map- Throws:
NullPointerException- if the specified map isnull
-
remove
Removes the mapping for a key from this map if it is present. Also removes the key from the set of accessed keys. -
size
public int size() -
isEmpty
public boolean isEmpty() -
equals
Compares the specified object with this map for equality. -
hashCode
public int hashCode() -
toString
-
clear
public void clear()Removes all the mappings from this map. The map will be empty after this call returns. Also clears the set of accessed keys. -
containsValue
Returnstrueif this map maps one or more keys to the specified value.- Specified by:
containsValuein interfaceMap<K,V> - Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif this map maps one or more keys to the specified value
-
values
- Specified by:
valuesin interfaceMap<K,V> - Returns:
- a
Collectionview of the values contained in this map
-
keySet
-
entrySet
-
expungeUnused
public void expungeUnused()Remove the entries from the Map that have not been accessed by .get() or .containsKey(). -
informAdditionalUsage
Adds the accessed keys from anotherTrackingMapto this map's set of accessed keys. This can be useful when merging usage information from multiple tracking maps.- Parameters:
additional- anotherTrackingMapwhose accessed keys are to be added
-
informAdditionalUsage
Add the used keys from the passed in TrackingMap to this TrackingMap's keysUsed. This can cause the readKeys to include entries that are not in wrapped Maps keys.- Parameters:
additional- TrackingMap whose used keys are to be added to this map's used keys.
-
keysUsed
- Returns:
- a
Setof accessed keys
-
getWrappedMap
Returns the underlyingMapthat thisTrackingMapwraps.- Returns:
- the wrapped
Map
-