Map implementation that stores data in simple fields until
the size is greater than 3.
This map is designed for performance and can outstrip HashMap. It also has good garbage collection characteristics.
The design uses two distinct modes of operation - flat and delegate. While the map is size 3 or less, operations map straight onto fields using switch statements. Once size 4 is reached, the map switches to delegate mode and only switches back when cleared. In delegate mode, all operations are forwarded straight to a HashMap resulting in the 5% performance loss.
The performance gains on puts are due to not needing to create a Map Entry object. This is a large saving not only in performance but in garbage collection.
Whilst in flat mode this map is also easy for the garbage collector to dispatch. This is because it contains no complex objects or arrays which slow the progress.
Do not use Flat3Map if the size is likely to grow beyond 3.
Note that Flat3Map is not synchronized and is not thread-safe.
If you wish to use this map from multiple threads concurrently, you must use
appropriate synchronization. The simplest approach is to wrap this map
using java.. This class may throw
exceptions when accessed by concurrent threads without synchronization.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv abstract static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private transient AbstractHashedMap | delegateMap
Map, used while in delegate mode |
| private transient int | hash1
Hash, used while in flat mode |
| private transient int | hash2
Hash, used while in flat mode |
| private transient int | hash3
Hash, used while in flat mode |
| private transient K | key1
Key, used while in flat mode |
| private transient K | key2
Key, used while in flat mode |
| private transient K | key3
Key, used while in flat mode |
| private static final long | serialVersionUID
Serialization version |
| private transient int | size
The size of the map, used while in flat mode |
| private transient V | value1
Value, used while in flat mode |
| private transient V | value2
Value, used while in flat mode |
| private transient V | value3
Value, used while in flat mode |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Implements java. |
| public Flat3Map | Returns: a shallow cloneOverrides java. |
| public boolean | Returns: true if the map contains the keythe key to search for key)Implements java. |
| public boolean | Returns: true if the map contains the keythe value to search for value)Implements java. |
| private void | |
| protected AbstractHashedMap | Returns: a new AbstractHashedMap or subclassCreate an instance of the map used for storage when in delegation mode. |
| public Set | Returns: the entrySet viewImplements java. |
| public boolean | Returns: true if equalthe object to compare to obj)Overrides java. Implements java. |
| public V | Returns: the mapped value, null if no matchthe key key)Implements java. |
| public int | Returns: the hash code defined in the Map interfaceOverrides java. Implements java. |
| public boolean | Returns: true if the map is currently size zeroImplements java. |
| public Set | Returns: the keySet viewImplements java. |
| public MapIterator | Returns: the map iteratorImplements org. |
| public V | Returns: the value previously mapped to this key, null if nonethe key to add key, final V the value to add value)Implements java. |
| public void | putAll(final Map<? extends K, ? extends V>
the map to add map)Implements java. |
| private void | |
| public V | Returns: the value mapped to the removed key, null if key not in mapthe mapping to remove key)Implements java. |
| public int | Returns: the sizeImplements java. |
| public String | Returns: a string version of the mapOverrides java. |
| public Collection | Returns: the values viewImplements java. |
| private void |