public class OpenHashMap<K,V> extends AbstractMap<K,V> implements Cloneable
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected static int |
defaultCapacity |
protected static double |
defaultMaxLoadFactor |
protected static double |
defaultMinLoadFactor |
protected int |
distinct
The number of distinct associations in the map; its "size()".
|
protected static Object |
FREE |
protected int |
freeEntries
The number of table entries in state==FREE.
|
protected int |
highWaterMark |
protected int |
lowWaterMark
The table capacity c=table.length always satisfies the invariant c * minLoadFactor <= s <= c *
maxLoadFactor, where s=size() is the number of associations currently contained.
|
protected double |
maxLoadFactor
The maximum load factor for the hashtable.
|
protected double |
minLoadFactor
The minimum load factor for the hashtable.
|
protected static Object |
REMOVED |
protected Object[] |
table
The hash table keys.
|
protected Object[] |
values
The hash table values.
|
Constructor and Description |
---|
OpenHashMap()
Constructs an empty map with default capacity and default load factors.
|
OpenHashMap(int initialCapacity)
Constructs an empty map with the specified initial capacity and default load factors.
|
OpenHashMap(int initialCapacity,
double minLoadFactor,
double maxLoadFactor)
Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.
|
Modifier and Type | Method and Description |
---|---|
protected int |
chooseGrowCapacity(int size,
double minLoad,
double maxLoad)
Chooses a new prime table capacity optimized for growing that (approximately) satisfies the invariant c *
minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.
|
protected int |
chooseHighWaterMark(int capacity,
double maxLoad)
Returns new high water mark threshold based on current capacity and maxLoadFactor.
|
protected int |
chooseLowWaterMark(int capacity,
double minLoad)
Returns new low water mark threshold based on current capacity and minLoadFactor.
|
protected int |
chooseShrinkCapacity(int size,
double minLoad,
double maxLoad)
Chooses a new prime table capacity optimized for shrinking that (approximately) satisfies the invariant c *
minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.
|
void |
clear()
Removes all (key,value) associations from the receiver.
|
OpenHashMap<K,V> |
clone()
Returns a deep copy of the receiver.
|
void |
concat() |
boolean |
containsKey(Object key)
Returns true if the receiver contains the specified key.
|
boolean |
containsValue(Object value)
Returns true if the receiver contains the specified value.
|
void |
ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new
internal memory.
|
Set<Map.Entry<K,V>> |
entrySet()
Allocate a set to contain Map.Entry objects for the pairs and return it.
|
protected static boolean |
equalsMindTheNull(Object a,
Object b) |
V |
get(Object key)
Returns the value associated with the specified key.
|
protected int |
indexOfInsertion(K key) |
protected int |
indexOfKey(K key) |
protected int |
indexOfValue(V value) |
protected int |
nextPrime(int desiredCapacity)
Returns a prime number which is
>= desiredCapacity and very close to desiredCapacity
(within 11% if desiredCapacity >= 1000 ). |
V |
put(K key,
V value)
Associates the given key with the given value.
|
protected void |
rehash(int newCapacity)
Rehashes the contents of the receiver into a new table with a smaller or larger capacity.
|
V |
remove(Object key)
Removes the given key with its associated element from the receiver, if present.
|
protected void |
setUp(int initialCapacity,
double minLoadFactor,
double maxLoadFactor)
Initializes the receiver.
|
int |
size()
Returns the number of (key,value) associations currently contained.
|
void |
trimToSize()
Trims the capacity of the receiver to be the receiver's current size.
|
equals, hashCode, isEmpty, keySet, putAll, toString, values
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
protected static final int defaultCapacity
protected static final double defaultMinLoadFactor
protected static final double defaultMaxLoadFactor
protected static final Object FREE
protected static final Object REMOVED
protected int distinct
protected int lowWaterMark
protected int highWaterMark
protected double minLoadFactor
protected double maxLoadFactor
protected Object[] table
protected Object[] values
protected int freeEntries
public OpenHashMap()
public OpenHashMap(int initialCapacity)
initialCapacity
- the initial capacity of the map.IllegalArgumentException
- if the initial capacity is less than zero.public OpenHashMap(int initialCapacity, double minLoadFactor, double maxLoadFactor)
initialCapacity
- the initial capacity.minLoadFactor
- the minimum load factor.maxLoadFactor
- the maximum load factor.IllegalArgumentException
- if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) ||
(maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >=
maxLoadFactor).public void clear()
public OpenHashMap<K,V> clone()
clone
in class AbstractMap<K,V>
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
public void ensureCapacity(int minCapacity)
This method never need be called; it is for performance tuning only. Calling this method before put()ing a large number of associations boosts performance, because the receiver will grow only once instead of potentially many times and hash collisions get less probable.
minCapacity
- the desired minimum capacity.public V get(Object key)
containsKey(Object)
whether the given key has a value associated or not, i.e. whether there exists an association
for the given key or not.protected int indexOfInsertion(K key)
key
- the key to be added to the receiver.protected int indexOfKey(K key)
key
- the key to be searched in the receiver.protected int indexOfValue(V value)
value
- the value to be searched in the receiver.public V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
key
- the key the value shall be associated with.value
- the value to be associated.protected void rehash(int newCapacity)
public V remove(Object key)
protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
initialCapacity
- the initial capacity of the receiver.minLoadFactor
- the minLoadFactor of the receiver.maxLoadFactor
- the maxLoadFactor of the receiver.IllegalArgumentException
- if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) ||
(maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >=
maxLoadFactor).public void trimToSize()
public void concat()
public Set<Map.Entry<K,V>> entrySet()
protected int chooseGrowCapacity(int size, double minLoad, double maxLoad)
protected int chooseHighWaterMark(int capacity, double maxLoad)
protected int chooseLowWaterMark(int capacity, double minLoad)
protected int chooseShrinkCapacity(int size, double minLoad, double maxLoad)
protected int nextPrime(int desiredCapacity)
>= desiredCapacity
and very close to desiredCapacity
(within 11% if desiredCapacity >= 1000
).desiredCapacity
- the capacity desired by the user.public int size()
Copyright © 2006–2015 The Apache Software Foundation. All rights reserved.