Class FixedSizeCache<K,V>
HashMap to
store keys and values while simultaneously registering the keys to an array to maintain a specified
maximum capacity.
As new elements are inserted into the cache, older ones may be removed as a result of the cache being at the maximum capacity set at instantiation.
- Since:
- 1.3
- Author:
- Michael Ritter
-
Constructor Summary
ConstructorsConstructorDescriptionFixedSizeCache(int size) Constructs a newFixedSizeCachewith a set maximum capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a key and pairs it with a value.booleanChecks if thisFixedSizeCachecontains a key.Retrieves a value from thisFixedSizeCachecorresponding to the specified key, ornullif there is no corresponding value to be retrieved.
-
Constructor Details
-
FixedSizeCache
public FixedSizeCache(int size) Constructs a newFixedSizeCachewith a set maximum capacity.This entity runs on the basis of "first-in-first-out", meaning that elements inserted into the newly constructed cache will remove the oldest ones if the maximum size is already being occupied.
- Parameters:
size- The size of the FixedSizeCache to be created.
-
-
Method Details
-
add
Adds a key and pairs it with a value.If this
FixedSizeCacheis already at maximum occupation, this will remove the oldest element.NOTE: Any inner workings of
HashMap#put(Object, Object)still apply when using this method!
It is recommended anyone using this consult the documentation for HashMap.- Parameters:
key- The key to pair with the valuevalue- The value to pair with the key- See Also:
-
contains
Checks if thisFixedSizeCachecontains a key.NOTE: Any inner workings of
HashMap#containsKey(Object)still apply when using this method!
It is recommended anyone using this consult the documentation for HashMap.- Parameters:
key- The key to check for- Returns:
trueif the FixedSizeCache contains a key, elsefalse- See Also:
-
get
Retrieves a value from thisFixedSizeCachecorresponding to the specified key, ornullif there is no corresponding value to be retrieved.NOTE: Any inner workings of
HashMap#get(Object)still apply when using this method!
It is recommended anyone using this consult the documentation for HashMap.- Parameters:
key- The key to retrieve a value for- Returns:
- A value corresponding to the provided key, or
nullif there was no value to get. - See Also:
-