Package gov.nasa.worldwind.util
Class BoundedHashMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- java.util.LinkedHashMap<K,V>
-
- gov.nasa.worldwind.util.BoundedHashMap<K,V>
-
- Type Parameters:
K
- The map key type.V
- The map value type.
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
public class BoundedHashMap<K,V> extends java.util.LinkedHashMap<K,V>
BoundedHashMap is a map with a fixed capacity. When the map's size exceeds its capacity, it automatically removes elements until its size is equal to its capacity.BoundedHashMap can operate in two ordering modes: insertion order and access order. The mode specified which entries are automatically removed when the map is over capacity. In insertion order mode the map removes the eldest entry (the first entry added). In access order mode, the map automatically removes the least recently used entry.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
DEFAULT_CAPACITY
protected static float
DEFAULT_LOAD_FACTOR
-
Constructor Summary
Constructors Constructor Description BoundedHashMap()
Creates a BoundedHashMap with a capacity of 16, in insertion order mode.BoundedHashMap(int capacity)
Creates a BoundedHashMap with a specified maximum capacity, in insertion order mode.BoundedHashMap(int capacity, boolean accessOrder)
Creates a BoundedHashMap with a specified maximum capacity and ordering mode.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getCapacity()
Returns the maximum number of entries in the map.protected static int
getInitialCapacity(int capacity, float loadFactor)
protected boolean
removeEldestEntry(java.util.Map.Entry<K,V> eldest)
protected void
removeOverCapacityEntries()
Removes the first n entries in the map, where n is the number of entries in the map beyond its capacity.void
setCapacity(int capacity)
Sets the maximum number of entries in the map.-
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
-
-
-
-
Field Detail
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
protected static final float DEFAULT_LOAD_FACTOR
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BoundedHashMap
public BoundedHashMap(int capacity, boolean accessOrder)
Creates a BoundedHashMap with a specified maximum capacity and ordering mode.- Parameters:
capacity
- the maximum number of entries in the map.accessOrder
- the ordering mode: true specifies access order, false specifies insertion order.
-
BoundedHashMap
public BoundedHashMap(int capacity)
Creates a BoundedHashMap with a specified maximum capacity, in insertion order mode.- Parameters:
capacity
- the maximum number of entries in the map.
-
BoundedHashMap
public BoundedHashMap()
Creates a BoundedHashMap with a capacity of 16, in insertion order mode.
-
-
Method Detail
-
getCapacity
public int getCapacity()
Returns the maximum number of entries in the map.- Returns:
- maximum number of entries in the map.
-
setCapacity
public void setCapacity(int capacity)
Sets the maximum number of entries in the map. If the new capacity is less than the map's current size, this automatically removes entries until the map's size is equal to its capacity.- Parameters:
capacity
- maximum number of entries in the map.
-
getInitialCapacity
protected static int getInitialCapacity(int capacity, float loadFactor)
-
removeOverCapacityEntries
protected void removeOverCapacityEntries()
Removes the first n entries in the map, where n is the number of entries in the map beyond its capacity. Note that the entry set and the corresponding iterator are backed by the map itself, so changes to an entry iterator correspond to changes in the map. We use the iterator's remove() method because we're removing elements from the entry set as we iterate over them.
-
-