public class FixedSizeCache<K,V>
extends java.lang.Object
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.
Constructor and Description |
---|
FixedSizeCache(int size)
Constructs a new
FixedSizeCache
with a set maximum capacity. |
Modifier and Type | Method and Description |
---|---|
void |
add(K key,
V value)
Adds a key and pairs it with a value.
|
boolean |
contains(K key)
Checks if this
FixedSizeCache
contains a key. |
V |
get(K key)
Retrieves a value from this
FixedSizeCache corresponding to the specified key, or null if there is no
corresponding value to be retrieved. |
public FixedSizeCache(int size)
FixedSizeCache
with 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.
size
- The size of the FixedSizeCache to be created.public void add(K key, V value)
If this FixedSizeCache
is 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.
key
- The key to pair with the valuevalue
- The value to pair with the keyHashMap#put(Object, Object)
public boolean contains(K key)
FixedSizeCache
contains 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.
key
- The key to check fortrue
if the FixedSizeCache contains a key, else false
HashMap#containsKey(Object)
public V get(K key)
FixedSizeCache
corresponding to the specified key, or null
if 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.
key
- The key to retrieve a value fornull
if there was no
value to get.HashMap#get(Object)