K
- Type of keysV
- Type of valuespublic class KeyComparatorHashMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.util.Map<K,V>, java.lang.Cloneable, java.io.Serializable
HashMap
but supports the
comparison of keys using a KeyComparator
.Modifier and Type | Class and Description |
---|---|
(package private) static class |
KeyComparatorHashMap.Entry<K,V> |
private class |
KeyComparatorHashMap.EntryIterator |
private class |
KeyComparatorHashMap.EntrySet |
private class |
KeyComparatorHashMap.HashIterator<E> |
private class |
KeyComparatorHashMap.KeyIterator |
private class |
KeyComparatorHashMap.ValueIterator |
Modifier and Type | Field and Description |
---|---|
(package private) static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity - MUST be a power of two.
|
(package private) static float |
DEFAULT_LOAD_FACTOR
The load factor used when none specified in constructor.
|
private java.util.Set<java.util.Map.Entry<K,V>> |
entrySet |
(package private) KeyComparator<K> |
keyComparator |
(package private) float |
loadFactor
The load factor for the hash table.
|
(package private) static int |
MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified
by either of the constructors with arguments.
|
(package private) int |
modCount
The number of times this HashMap has been structurally modified
Structural modifications are those that change the number of mappings in
the HashMap or otherwise modify its internal structure (e.g.,
rehash).
|
(package private) static java.lang.Object |
NULL_KEY
Value representing null keys inside tables.
|
private static long |
serialVersionUID |
(package private) int |
size
The number of key-value mappings contained in this identity hash map.
|
(package private) KeyComparatorHashMap.Entry<K,V>[] |
table
The table, resized as necessary.
|
(package private) int |
threshold
The next ss value at which to resize (capacity * load factor).
|
Constructor and Description |
---|
KeyComparatorHashMap(int initialCapacity,
float loadFactor,
KeyComparator<K> keyComparator)
Constructs an empty HashMap with the specified initial
capacity and load factor.
|
KeyComparatorHashMap(int initialCapacity,
KeyComparator<K> keyComparator)
Constructs an empty HashMap with the specified initial
capacity and the default load factor (0.75).
|
KeyComparatorHashMap(KeyComparator<K> keyComparator)
Constructs an empty HashMap with the default initial capacity
(16) and the default load factor (0.75).
|
KeyComparatorHashMap(java.util.Map<? extends K,? extends V> m,
KeyComparator<K> keyComparator)
Constructs a new HashMap with the same mappings as the
specified Map.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addEntry(int hash,
K key,
V value,
int bucketIndex)
Add a new entry with the specified key, value and hash code to
the specified bucket.
|
(package private) int |
capacity() |
void |
clear()
Removes all mappings from this map.
|
java.lang.Object |
clone()
Returns a shallow copy of this HashMap instance: the keys and
values themselves are not cloned.
|
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the
specified key.
|
private boolean |
containsNullValue()
Special-case code for containsValue with null argument
|
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the
specified value.
|
(package private) void |
createEntry(int hash,
K key,
V value,
int bucketIndex)
Like addEntry except that this version is used when creating entries
as part of Map construction or "pseudo-construction" (cloning,
deserialization).
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Returns a collection view of the mappings contained in this map.
|
(package private) static boolean |
eq(java.lang.Object x,
java.lang.Object y)
Check for equality of non-null reference x and possibly-null y.
|
V |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this identity
hash map, or null if the map contains no mapping for this key.
|
(package private) KeyComparatorHashMap.Entry<K,V> |
getEntry(K key)
Returns the entry associated with the specified key in the
HashMap.
|
int |
getModCount()
Get the number of times this HashMap has been structurally modified
Structural modifications are those that change the number of mappings in
the HashMap or otherwise modify its internal structure (e.g.,
rehash).
|
(package private) int |
hash(int h) |
(package private) static int |
hash(java.lang.Object x)
Returns a hash value for the specified object.
|
(package private) static int |
indexFor(int h,
int length)
Returns index for hash code h.
|
(package private) void |
init()
Initialization hook for subclasses.
|
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
(package private) static <T> boolean |
isNull(T key) |
(package private) boolean |
keyComparatorEq(K x,
K y)
Check for equality of non-null reference x and possibly-null y.
|
(package private) int |
keyComparatorHash(K k) |
(package private) float |
loadFactor() |
(package private) static <T> T |
maskNull(T key)
Returns internal representation for key.
|
(package private) java.util.Iterator<java.util.Map.Entry<K,V>> |
newEntryIterator() |
(package private) java.util.Iterator<K> |
newKeyIterator() |
(package private) java.util.Iterator<V> |
newValueIterator() |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
void |
putAll(java.util.Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map
These mappings will replace any mappings that
this map had for any of the keys currently in the specified map.
|
private void |
putAllForCreate(java.util.Map<? extends K,? extends V> m) |
private void |
putForCreate(K key,
V value)
This method is used instead of put by constructors and
pseudoconstructors (clone, readObject).
|
private void |
readObject(java.io.ObjectInputStream s)
Reconstitute the HashMap instance from a stream (i.e.,
deserialize it).
|
V |
remove(java.lang.Object key)
Removes the mapping for this key from this map if present.
|
(package private) KeyComparatorHashMap.Entry<K,V> |
removeEntryForKey(java.lang.Object key)
Removes and returns the entry associated with the specified key
in the HashMap.
|
(package private) KeyComparatorHashMap.Entry<K,V> |
removeMapping(java.lang.Object o)
Special version of remove for EntrySet.
|
(package private) void |
resize(int newCapacity)
Rehashes the contents of this map into a new array with a
larger capacity.
|
int |
size()
Returns the number of key-value mappings in this map.
|
(package private) void |
transfer(KeyComparatorHashMap.Entry<K,V>[] newTable)
Transfer all entries from current table to newTable.
|
(package private) static <T> T |
unmaskNull(T key)
Returns key represented by specified internal representation.
|
private void |
writeObject(java.io.ObjectOutputStream s)
Save the state of the HashMap instance to a stream (i.e.,
serialize it).
|
private static final long serialVersionUID
static final int DEFAULT_INITIAL_CAPACITY
static final int MAXIMUM_CAPACITY
static final float DEFAULT_LOAD_FACTOR
transient KeyComparatorHashMap.Entry<K,V>[] table
transient int size
int threshold
final float loadFactor
transient volatile int modCount
final KeyComparator<K> keyComparator
static final java.lang.Object NULL_KEY
public KeyComparatorHashMap(int initialCapacity, float loadFactor, KeyComparator<K> keyComparator)
initialCapacity
- The initial capacity.loadFactor
- The load factor.keyComparator
- the map key comparator.java.lang.IllegalArgumentException
- if the initial capacity is negative
or the load factor is nonpositive.public KeyComparatorHashMap(int initialCapacity, KeyComparator<K> keyComparator)
initialCapacity
- the initial capacity.keyComparator
- the map key comparator.java.lang.IllegalArgumentException
- if the initial capacity is negative.public KeyComparatorHashMap(KeyComparator<K> keyComparator)
keyComparator
- the map key comparator.public KeyComparatorHashMap(java.util.Map<? extends K,? extends V> m, KeyComparator<K> keyComparator)
m
- the map whose mappings are to be placed in this map.keyComparator
- the comparatorjava.lang.NullPointerException
- if the specified map is null.public int getModCount()
void init()
static <T> T maskNull(T key)
static <T> boolean isNull(T key)
static <T> T unmaskNull(T key)
static int hash(java.lang.Object x)
The shift distances in this function were chosen as the result of an automated search over the entire four-dimensional search space.
static boolean eq(java.lang.Object x, java.lang.Object y)
static int indexFor(int h, int length)
public int size()
public boolean isEmpty()
int keyComparatorHash(K k)
int hash(int h)
boolean keyComparatorEq(K x, K y)
public V get(java.lang.Object key)
get
in interface java.util.Map<K,V>
get
in class java.util.AbstractMap<K,V>
key
- the key whose associated value is to be returned.put(Object, Object)
public boolean containsKey(java.lang.Object key)
KeyComparatorHashMap.Entry<K,V> getEntry(K key)
public V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.private void putForCreate(K key, V value)
void resize(int newCapacity)
newCapacity
- the new capacity, MUST be a power of two;
must be greater than current capacity unless current
capacity is MAXIMUM_CAPACITY (in which case value
is irrelevant).void transfer(KeyComparatorHashMap.Entry<K,V>[] newTable)
public void putAll(java.util.Map<? extends K,? extends V> m)
public V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
remove
in class java.util.AbstractMap<K,V>
key
- key whose mapping is to be removed from the map.KeyComparatorHashMap.Entry<K,V> removeEntryForKey(java.lang.Object key)
KeyComparatorHashMap.Entry<K,V> removeMapping(java.lang.Object o)
public void clear()
public boolean containsValue(java.lang.Object value)
private boolean containsNullValue()
public java.lang.Object clone()
void addEntry(int hash, K key, V value, int bucketIndex)
void createEntry(int hash, K key, V value, int bucketIndex)
java.util.Iterator<K> newKeyIterator()
java.util.Iterator<V> newValueIterator()
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
int capacity()
float loadFactor()