V
- The value type stored in the map.public class IntObjectHashMap<V> extends java.lang.Object implements IntObjectMap<V>
IntObjectMap
that uses open addressing for keys.
To minimize the memory footprint, this class uses open addressing rather than chaining.
Collisions are resolved using linear probing. Deletions implement compaction, so cost of
remove can approach O(N) for full maps, which makes a small loadFactor recommended.Modifier and Type | Class and Description |
---|---|
private class |
IntObjectHashMap.EntrySet
Set implementation for iterating over the entries of the map.
|
private class |
IntObjectHashMap.KeySet
Set implementation for iterating over the keys.
|
(package private) class |
IntObjectHashMap.MapEntry
A single entry in the map.
|
private class |
IntObjectHashMap.MapIterator
Iterator used by the
Map interface. |
private class |
IntObjectHashMap.PrimitiveIterator
Iterator over primitive entries.
|
IntObjectMap.PrimitiveEntry<V>
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY
Default initial capacity.
|
static float |
DEFAULT_LOAD_FACTOR
Default load factor.
|
private java.lang.Iterable<IntObjectMap.PrimitiveEntry<V>> |
entries |
private java.util.Set<java.util.Map.Entry<java.lang.Integer,V>> |
entrySet |
private int[] |
keys |
private java.util.Set<java.lang.Integer> |
keySet |
private float |
loadFactor
The load factor for the map.
|
private int |
mask |
private int |
maxSize
The maximum number of elements allowed without allocating more space.
|
private static java.lang.Object |
NULL_VALUE
Placeholder for null values, so we can use the actual null to mean available.
|
private int |
size |
private V[] |
values |
Constructor and Description |
---|
IntObjectHashMap() |
IntObjectHashMap(int initialCapacity) |
IntObjectHashMap(int initialCapacity,
float loadFactor) |
Modifier and Type | Method and Description |
---|---|
private int |
calcMaxSize(int capacity)
Calculates the maximum size allowed before rehashing.
|
void |
clear() |
boolean |
containsKey(int key)
Indicates whether or not this map contains a value for the specified key.
|
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
java.lang.Iterable<IntObjectMap.PrimitiveEntry<V>> |
entries()
Gets an iterable to traverse over the primitive entries contained in this map.
|
java.util.Set<java.util.Map.Entry<java.lang.Integer,V>> |
entrySet() |
boolean |
equals(java.lang.Object obj) |
V |
get(int key)
Gets the value in the map with the specified key.
|
V |
get(java.lang.Object key) |
private void |
growSize()
Grows the map size after an insertion.
|
int |
hashCode() |
private static int |
hashCode(int key)
Returns the hash code for the key.
|
private int |
hashIndex(int key)
Returns the hashed index for the given key.
|
private int |
indexOf(int key)
Locates the index for the given key.
|
boolean |
isEmpty() |
java.util.Set<java.lang.Integer> |
keySet() |
protected java.lang.String |
keyToString(int key)
Helper method called by
toString() in order to convert a single map key into a string. |
private int |
objectToKey(java.lang.Object key) |
private int |
probeNext(int index)
Get the next sequential index after
index and wraps if necessary. |
V |
put(java.lang.Integer key,
V value) |
V |
put(int key,
V value)
Puts the given entry into the map.
|
void |
putAll(java.util.Map<? extends java.lang.Integer,? extends V> sourceMap) |
private void |
rehash(int newCapacity)
Rehashes the map for the given capacity.
|
V |
remove(int key)
Removes the entry with the specified key.
|
V |
remove(java.lang.Object key) |
private boolean |
removeAt(int index)
Removes entry at the given index position.
|
int |
size() |
private static <T> T |
toExternal(T value) |
private static <T> T |
toInternal(T value) |
java.lang.String |
toString() |
java.util.Collection<V> |
values() |
public static final int DEFAULT_CAPACITY
public static final float DEFAULT_LOAD_FACTOR
private static final java.lang.Object NULL_VALUE
private int maxSize
private final float loadFactor
maxSize
.private int[] keys
private V[] values
private int size
private int mask
private final java.util.Set<java.lang.Integer> keySet
private final java.util.Set<java.util.Map.Entry<java.lang.Integer,V>> entrySet
private final java.lang.Iterable<IntObjectMap.PrimitiveEntry<V>> entries
public IntObjectHashMap()
public IntObjectHashMap(int initialCapacity)
public IntObjectHashMap(int initialCapacity, float loadFactor)
private static <T> T toExternal(T value)
private static <T> T toInternal(T value)
public V get(int key)
IntObjectMap
get
in interface IntObjectMap<V>
key
- the key whose associated value is to be returned.null
if the key was not found in the map.public V put(int key, V value)
IntObjectMap
put
in interface IntObjectMap<V>
key
- the key of the entry.value
- the value of the entry.null
if there was no previous mapping.public void putAll(java.util.Map<? extends java.lang.Integer,? extends V> sourceMap)
putAll
in interface java.util.Map<java.lang.Integer,V>
public V remove(int key)
IntObjectMap
remove
in interface IntObjectMap<V>
key
- the key for the entry to be removed from this map.null
if there was no mapping.public int size()
size
in interface java.util.Map<java.lang.Integer,V>
public boolean isEmpty()
isEmpty
in interface java.util.Map<java.lang.Integer,V>
public void clear()
clear
in interface java.util.Map<java.lang.Integer,V>
public boolean containsKey(int key)
IntObjectMap
containsKey
in interface IntObjectMap<V>
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<java.lang.Integer,V>
public java.lang.Iterable<IntObjectMap.PrimitiveEntry<V>> entries()
IntObjectMap
IntObjectMap.PrimitiveEntry
s returned by the Iterator
may change as the Iterator
progresses. The caller should not rely on IntObjectMap.PrimitiveEntry
key/value stability.entries
in interface IntObjectMap<V>
public java.util.Collection<V> values()
values
in interface java.util.Map<java.lang.Integer,V>
public int hashCode()
hashCode
in interface java.util.Map<java.lang.Integer,V>
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in interface java.util.Map<java.lang.Integer,V>
equals
in class java.lang.Object
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<java.lang.Integer,V>
public V get(java.lang.Object key)
get
in interface java.util.Map<java.lang.Integer,V>
public V put(java.lang.Integer key, V value)
put
in interface java.util.Map<java.lang.Integer,V>
public V remove(java.lang.Object key)
remove
in interface java.util.Map<java.lang.Integer,V>
public java.util.Set<java.lang.Integer> keySet()
keySet
in interface java.util.Map<java.lang.Integer,V>
public java.util.Set<java.util.Map.Entry<java.lang.Integer,V>> entrySet()
entrySet
in interface java.util.Map<java.lang.Integer,V>
private int objectToKey(java.lang.Object key)
private int indexOf(int key)
key
- the key for an entry in the map.-1
if no entry is found for that key.private int hashIndex(int key)
private static int hashCode(int key)
private int probeNext(int index)
index
and wraps if necessary.private void growSize()
private boolean removeAt(int index)
index
- the index position of the element to remove.true
if the next item was moved back. false
otherwise.private int calcMaxSize(int capacity)
private void rehash(int newCapacity)
newCapacity
- the new capacity for the map.public java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.String keyToString(int key)
toString()
in order to convert a single map key into a string.
This is protected to allow subclasses to override the appearance of a given key.