containsKey(k), put(k, v) and remove(k) are all (expected and
amortized) constant time operations. Expected in the hashtable sense (depends on the hash
function doing a good job of distributing the elements to the buckets to a distribution not far
from uniform), and amortized since some operations can trigger a hash table resize.
Unlike java.util.HashMap, iteration is only proportional to the actual size(),
which is optimal, and not the size of the internal hashtable, which could be much larger
than size(). Furthermore, this structure places significantly reduced load on the garbage
collector by only using a constant number of internal objects.
If there are no removals, then iteration order for the entrySet, keySet, and
values views is the same as insertion order. Any removal invalidates any ordering
guarantees.
This class should not be assumed to be universally superior to java.util.HashMap.
Generally speaking, this class reduces object allocation and memory consumption at the price of
moderately increased constant factors of CPU. Only use this class when there is a specific reason
to prioritize memory over CPU.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv class | |
| private abstract class | |
| pack-priv class | |
| pack-priv class | |
| pack-priv class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv transient int[] | entries
Contains the logical entries, in the range of [0, size()). |
| private transient Set | |
| pack-priv static final double | HASH_FLOODING_FPP
Maximum allowed false positive probability of detecting a hash flooding attack given random input. |
| pack-priv transient @Nullable Object[] | keys
The keys of the entries in the map, in the range of [0, size()). |
| private transient Set | |
| private static final int | MAX_HASH_BUCKET_LENGTH
Maximum allowed length of a hash table bucket before falling back to a j.u.LinkedHashMap-based implementation. |
| private transient int | metadata
Keeps track of metadata like the number of hash table bits and modifications of this data structure (to make it possible to throw ConcurrentModificationException in the iterator). |
| private static final Object | |
| private transient int | size
The number of elements contained in the set. |
| private transient Object | table
The hashtable object. |
| pack-priv transient @Nullable Object[] | values
Hides java. |
| private transient Collection |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv | CompactHashMap(int
the initial capacity of this expectedSize)CompactHashMap.Constructs a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| pack-priv int | adjustAfterRemove(int indexBeforeRemove, int indexRemoved)
Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at. |
| pack-priv int | |
| public void | clear()
Overrides java. Implements java. |
| public boolean | containsKey(Object
key whose presence in this map is to be tested key)Overrides java. Implements java. true if this map contains a mapping for the specified
key.
|
| public boolean | containsValue(Object
value whose presence in this map is to be tested value)Overrides java. Implements java. true if this map maps one or more keys to the
specified value.
|
| pack-priv Map | |
| public static <K extends @Nullable Object, V extends @Nullable Object> CompactHashMap | |
| pack-priv Set | |
| pack-priv Map | |
| pack-priv Set | |
| pack-priv Collection | |
| public static <K extends @Nullable Object, V extends @Nullable Object> CompactHashMap | Returns: a new, emptyCompactHashMap with enough capacity to hold expectedSize
elements without resizingthe number of elements you expect to add to the returned set expectedSize)Creates a |
| pack-priv Map | |
| private int | |
| public Set | entrySet()
Implements abstract java. Implements java. Set view of the mappings contained in this map.
|
| pack-priv Iterator | |
| pack-priv int | |
| public void | forEach(BiConsumer<? super K, ? super V>
The action to be performed for each entry action)Overrides default java. |
| public V | get(Object
the key whose associated value is to be returned key)Overrides java. Implements java. null if this map contains no mapping for the key.
|
| pack-priv int | |
| private int | |
| pack-priv void | |
| private int | |
| pack-priv void | |
| pack-priv void | insertEntry(int entryIndex, K key, V value, int hash, int mask)
Creates a fresh entry with the specified object at the specified position in the entry arrays. |
| public boolean | isEmpty()
Overrides java. Implements java. true if this map contains no key-value mappings.
|
| private K | |
| public Set | keySet()
Overrides java. Implements java. Set view of the keys contained in this map.
|
| pack-priv Iterator | |
| pack-priv void | moveLastEntry(int dstIndex, int mask)
Moves the last entry in the entry array into |
| pack-priv boolean | |
| public V | put(K
key with which the specified value is to be associated key, V value to be associated with the specified key value)Overrides java. Implements java. |
| private void | |
| public V | remove(Object
key whose mapping is to be removed from the map key)Overrides java. Implements java. |
| private @Nullable Object | |
| public void | replaceAll(BiFunction<? super K, ? super V, ? extends V>
the function to apply to each entry function)Overrides default java. |
| private int[] | |
| private @Nullable Object[] | |
| private Object | |
| private @Nullable Object[] | |
| pack-priv void | resizeEntries(int newCapacity)
Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity. |
| private void | |
| private int | |
| private void | |
| private void | setHashTableMask(int mask)
Stores the hash table mask as the number of bits needed to represent an index. |
| private void | |
| private void | |
| public int | size()
Overrides java. Implements java. |
| public void | trimToSize()
Ensures that this |
| private V | |
| public Collection | values()
Overrides java. Implements java. Collection view of the values contained in this map.
|
| pack-priv Iterator | |
| private void |