This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.
Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
| Modifier and Type | Class and Description |
|---|---|
| public static class | |
| public static class | |
| public static class | |
| private static class | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv transient ObjectIntMap. | |
| pack-priv transient ObjectIntMap. | |
| pack-priv transient ObjectIntMap. | |
| pack-priv transient ObjectIntMap. | |
| pack-priv K[] | |
| pack-priv float | |
| protected int | mask
A bitmask used to confine hashcodes to the size of the table. |
| protected int | shift
Used by |
| public int | |
| pack-priv int | |
| pack-priv transient ObjectIntMap. | |
| pack-priv transient ObjectIntMap. | |
| pack-priv int[] |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ObjectIntMap(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity)Creates a new map with a load factor of 0.8. |
| public | ObjectIntMap(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity, float loadFactor)Creates a new map with the specified initial capacity and load factor. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. |
| public void | |
| public boolean | |
| public boolean | |
| public void | ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. |
| public ObjectIntMap. | |
| public boolean | equals(Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public K | |
| public int | get(K key, int defaultValue)
Returns the value for the specified key, or the default value if the key is not in the map. |
| public int | getAndIncrement(K key, int defaultValue, int increment)
Returns the key's current value and increments the stored value. |
| public int | |
| public boolean | |
| public ObjectIntMap. | |
| public ObjectIntMap. | |
| pack-priv int | locateKey(K key)
Returns the index of the key if already present, else -(index + 1) for the next empty index. |
| public boolean | |
| protected int | |
| public void | |
| public int | put(K key, int value, int defaultValue)
Returns the old value associated with the specified key, or the specified default value. |
| public void | |
| public void | |
| public int | |
| private void | |
| public int | remove(K key, int defaultValue)
Returns the value for the removed key, or the default value if the key is not in the map. |
| pack-priv final void | |
| public void | shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. |
| public String | |
| public String | |
| private String | |
| public ObjectIntMap. |
| entries1 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| entries2 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| keys1 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| keys2 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| keyTable | back to summary |
|---|---|
| pack-priv K[] keyTable | |
| loadFactor | back to summary |
|---|---|
| pack-priv float loadFactor | |
| mask | back to summary |
|---|---|
| protected int mask A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two
minus 1. If | |
| shift | back to summary |
|---|---|
| protected int shift Used by
| |
| size | back to summary |
|---|---|
| public int size | |
| threshold | back to summary |
|---|---|
| pack-priv int threshold | |
| values1 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| values2 | back to summary |
|---|---|
| pack-priv transient ObjectIntMap. | |
| valueTable | back to summary |
|---|---|
| pack-priv int[] valueTable | |
| ObjectIntMap | back to summary |
|---|---|
| public ObjectIntMap() Creates a new map with an initial capacity of 51 and a load factor of 0.8. | |
| ObjectIntMap | back to summary |
|---|---|
| public ObjectIntMap(int initialCapacity) Creates a new map with a load factor of 0.8.
| |
| ObjectIntMap | back to summary |
|---|---|
| public ObjectIntMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.
| |
| ObjectIntMap | back to summary |
|---|---|
| public ObjectIntMap(ObjectIntMap<? extends K> map) Creates a new map identical to the specified map. | |
| clear | back to summary |
|---|---|
| public void clear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. | |
| clear | back to summary |
|---|---|
| public void clear() | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(K key) | |
| containsValue | back to summary |
|---|---|
| public boolean containsValue(int value) Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be an expensive operation. | |
| ensureCapacity | back to summary |
|---|---|
| public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes. | |
| entries | back to summary |
|---|---|
| public ObjectIntMap. Returns an iterator for the entries in the map. Remove is supported.
If | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| findKey | back to summary |
|---|---|
| public K findKey(int value) Returns the key for the specified value, or null if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
| |
| get | back to summary |
|---|---|
| public int get(K key, int defaultValue) Returns the value for the specified key, or the default value if the key is not in the map. | |
| getAndIncrement | back to summary |
|---|---|
| public int getAndIncrement(K key, int defaultValue, int increment) Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Returns true if the map is empty. | |
| iterator | back to summary |
|---|---|
| public ObjectIntMap. Implements java. Doc from java. Returns an iterator over elements of type | |
| keys | back to summary |
|---|---|
| public ObjectIntMap. Returns an iterator for the keys in the map. Remove is supported.
If | |
| locateKey | back to summary |
|---|---|
| pack-priv int locateKey(K key) Returns the index of the key if already present, else -(index + 1) for the next empty index. This can be overridden in this
pacakge to compare for equality differently than | |
| notEmpty | back to summary |
|---|---|
| public boolean notEmpty() Returns true if the map has one or more items. | |
| place | back to summary |
|---|---|
| protected int place(K item) Returns an index >= 0 and <=
The default implementation uses Fibonacci hashing on the item's
This method can be overridden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are
Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality
hashcodes and don't need Fibonacci hashing: | |
| put | back to summary |
|---|---|
| public void put(K key, int value) | |
| put | back to summary |
|---|---|
| public int put(K key, int value, int defaultValue) Returns the old value associated with the specified key, or the specified default value. | |
| putAll | back to summary |
|---|---|
| public void putAll(ObjectIntMap<? extends K> map) | |
| putMissing | back to summary |
|---|---|
| public void putMissing(K key, int value) | |
| putMissing | back to summary |
|---|---|
| public int putMissing(K key, int value, int defaultValue) | |
| putResize | back to summary |
|---|---|
| private void putResize(K key, int value) Skips checks for existing keys, doesn't increment size. | |
| remove | back to summary |
|---|---|
| public int remove(K key, int defaultValue) Returns the value for the removed key, or the default value if the key is not in the map. | |
| resize | back to summary |
|---|---|
| pack-priv final void resize(int newSize) | |
| shrink | back to summary |
|---|---|
| public void shrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead. | |
| toString | back to summary |
|---|---|
| public String toString(String separator) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| toString | back to summary |
|---|---|
| private String toString(String separator, boolean braces) | |
| values | back to summary |
|---|---|
| public ObjectIntMap. Returns an iterator for the values in the map. Remove is supported.
If | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv ObjectIntMap. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public ObjectIntMap. | |
| public ObjectIntMap. | next()
Implements java. Note the same entry instance is returned each time this method is called. |
| entry | back to summary |
|---|---|
| pack-priv ObjectIntMap. | |
| Entries | back to summary |
|---|---|
| public Entries(ObjectIntMap<K> map) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns | |
| iterator | back to summary |
|---|---|
| public ObjectIntMap. Implements java. Doc from java. Returns an iterator over elements of type | |
| next | back to summary |
|---|---|
| public ObjectIntMap. Implements java. Note the same entry instance is returned each time this method is called. | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String |
| Entry | back to summary |
|---|---|
| public Entry() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public ObjectIntMap. | |
| public K | |
| public Array | |
| public Array |
| Keys | back to summary |
|---|---|
| public Keys(ObjectIntMap<K> map) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns | |
| iterator | back to summary |
|---|---|
| public ObjectIntMap. Implements java. Doc from java. Returns an iterator over elements of type | |
| next | back to summary |
|---|---|
| public K next() Implements java. Doc from java. Returns the next element in the iteration. | |
| toArray | back to summary |
|---|---|
| public Array Returns a new array containing the remaining keys. | |
| toArray | back to summary |
|---|---|
| public Array Adds the remaining keys to the array. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| public boolean | |
| pack-priv final ObjectIntMap | |
| pack-priv int | |
| pack-priv boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public void | |
| public void |
| currentIndex | back to summary |
|---|---|
| pack-priv int currentIndex | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext | |
| map | back to summary |
|---|---|
| pack-priv final ObjectIntMap<K> map | |
| nextIndex | back to summary |
|---|---|
| pack-priv int nextIndex | |
| valid | back to summary |
|---|---|
| pack-priv boolean valid | |
| MapIterator | back to summary |
|---|---|
| public MapIterator(ObjectIntMap<K> map) | |
| findNextIndex | back to summary |
|---|---|
| pack-priv void findNextIndex() | |
| remove | back to summary |
|---|---|
| public void remove() | |
| reset | back to summary |
|---|---|
| public void reset() | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public ObjectIntMap. | |
| public int | |
| public IntArray | |
| public IntArray |
| Values | back to summary |
|---|---|
| public Values(ObjectIntMap<?> map) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() | |
| iterator | back to summary |
|---|---|
| public ObjectIntMap. | |
| next | back to summary |
|---|---|
| public int next() | |
| toArray | back to summary |
|---|---|
| public IntArray toArray() Returns a new array containing the remaining values. | |
| toArray | back to summary |
|---|---|
| public IntArray toArray(IntArray array) Adds the remaining values to the specified array. | |