| Method from java.util.HashMap Detail: |
static boolean areEqualKeys(Object key1,
Object key2) {
return (key1 == key2) || key1.equals(key2);
}
|
static boolean areEqualValues(Object value1,
Object value2) {
return (value1 == value2) || value1.equals(value2);
}
|
public void clear() {
if (elementCount > 0) {
elementCount = 0;
Arrays.fill(elementData, null);
modCount++;
}
}
Removes all mappings from this hash map, leaving it empty. |
public Object clone() {
try {
HashMap< K, V > map = (HashMap< K, V >) super.clone();
map.elementCount = 0;
map.elementData = newElementArray(elementData.length);
map.putAll(this);
return map;
} catch (CloneNotSupportedException e) {
return null;
}
}
Returns a shallow copy of this map. |
static int computeHashCode(Object key) {
return key.hashCode();
}
|
public boolean containsKey(Object key) {
Entry< K, V > m = getEntry(key);
return m != null;
}
Returns whether this map contains the specified key. |
public boolean containsValue(Object value) {
if (value != null) {
for (int i = 0; i < elementData.length; i++) {
Entry< K, V > entry = elementData[i];
while (entry != null) {
if (areEqualValues(value, entry.value)) {
return true;
}
entry = entry.next;
}
}
} else {
for (int i = 0; i < elementData.length; i++) {
Entry< K, V > entry = elementData[i];
while (entry != null) {
if (entry.value == null) {
return true;
}
entry = entry.next;
}
}
}
return false;
}
Returns whether this map contains the specified value. |
Entry<K, V> createEntry(K key,
int index,
V value) {
Entry< K, V > entry = new Entry< K, V >(key, value);
entry.next = elementData[index];
elementData[index] = entry;
return entry;
}
|
Entry<K, V> createHashedEntry(K key,
int index,
int hash) {
Entry< K,V > entry = new Entry< K,V >(key,hash);
entry.next = elementData[index];
elementData[index] = entry;
return entry;
}
|
public Set<K, V> entrySet() {
return new HashMapEntrySet< K, V >(this);
}
Returns a set containing all of the mappings in this map. Each mapping is
an instance of Map.Entry . As the set is backed by this map,
changes in one will be reflected in the other. |
final Entry<K, V> findNonNullKeyEntry(Object key,
int index,
int keyHash) {
Entry< K,V > m = elementData[index];
while (m != null
&& (m.origKeyHash != keyHash || !areEqualKeys(key, m.key))) {
m = m.next;
}
return m;
}
|
final Entry<K, V> findNullKeyEntry() {
Entry< K,V > m = elementData[0];
while (m != null && m.key != null)
m = m.next;
return m;
}
|
public V get(Object key) {
Entry< K, V > m = getEntry(key);
if (m != null) {
return m.value;
}
return null;
}
Returns the value of the mapping with the specified key. |
final Entry<K, V> getEntry(Object key) {
Entry< K, V > m;
if (key == null) {
m = findNullKeyEntry();
} else {
int hash = computeHashCode(key);
int index = hash & (elementData.length - 1);
m = findNonNullKeyEntry(key, index, hash);
}
return m;
}
|
public boolean isEmpty() {
return elementCount == 0;
}
Returns whether this map is empty. |
public Set<K> keySet() {
if (keySet == null) {
keySet = new AbstractSet< K >() {
@Override
public boolean contains(Object object) {
return containsKey(object);
}
@Override
public int size() {
return HashMap.this.size();
}
@Override
public void clear() {
HashMap.this.clear();
}
@Override
public boolean remove(Object key) {
Entry< K, V > entry = HashMap.this.removeEntry(key);
return entry != null;
}
@Override
public Iterator< K > iterator() {
return new KeyIterator< K,V > (HashMap.this);
}
};
}
return keySet;
}
Returns a set of the keys contained in this map. The set is backed by
this map so changes to one are reflected by the other. The set does not
support adding. |
Entry<K, V>[] newElementArray(int s) {
return new Entry[s];
}
Create a new element array |
public V put(K key,
V value) {
return putImpl(key, value);
}
Maps the specified key to the specified value. |
public void putAll(Map<? extends K, ? extends V> map) {
if (!map.isEmpty()) {
putAllImpl(map);
}
}
Copies all the mappings in the specified map to this map. These mappings
will replace all mappings that this map had for any of the keys currently
in the given map. |
V putImpl(K key,
V value) {
Entry< K,V > entry;
if(key == null) {
entry = findNullKeyEntry();
if (entry == null) {
modCount++;
if (++elementCount > threshold) {
rehash();
}
entry = createHashedEntry(null, 0, 0);
}
} else {
int hash = computeHashCode(key);
int index = hash & (elementData.length - 1);
entry = findNonNullKeyEntry(key, index, hash);
if (entry == null) {
modCount++;
if (++elementCount > threshold) {
rehash();
index = hash & (elementData.length - 1);
}
entry = createHashedEntry(key, index, hash);
}
}
V result = entry.value;
entry.value = value;
return result;
}
|
void rehash() {
rehash(elementData.length);
}
|
void rehash(int capacity) {
int length = calculateCapacity((capacity == 0 ? 1 : capacity < < 1));
Entry< K, V >[] newData = newElementArray(length);
for (int i = 0; i < elementData.length; i++) {
Entry< K, V > entry = elementData[i];
while (entry != null) {
int index = entry.origKeyHash & (length - 1);
Entry< K, V > next = entry.next;
entry.next = newData[index];
newData[index] = entry;
entry = next;
}
}
elementData = newData;
computeThreshold();
}
|
public V remove(Object key) {
Entry< K, V > entry = removeEntry(key);
if (entry != null) {
return entry.value;
}
return null;
}
Removes the mapping with the specified key from this map. |
final void removeEntry(Entry<K, V> entry) {
int index = entry.origKeyHash & (elementData.length - 1);
Entry< K, V > m = elementData[index];
if (m == entry) {
elementData[index] = entry.next;
} else {
while (m.next != entry) {
m = m.next;
}
m.next = entry.next;
}
modCount++;
elementCount--;
}
|
final Entry<K, V> removeEntry(Object key) {
int index = 0;
Entry< K, V > entry;
Entry< K, V > last = null;
if (key != null) {
int hash = computeHashCode(key);
index = hash & (elementData.length - 1);
entry = elementData[index];
while (entry != null && !(entry.origKeyHash == hash && areEqualKeys(key, entry.key))) {
last = entry;
entry = entry.next;
}
} else {
entry = elementData[0];
while (entry != null && entry.key != null) {
last = entry;
entry = entry.next;
}
}
if (entry == null) {
return null;
}
if (last == null) {
elementData[index] = entry.next;
} else {
last.next = entry.next;
}
modCount++;
elementCount--;
return entry;
}
|
public int size() {
return elementCount;
}
Returns the number of elements in this map. |
public Collection<V> values() {
if (valuesCollection == null) {
valuesCollection = new AbstractCollection< V >() {
@Override
public boolean contains(Object object) {
return containsValue(object);
}
@Override
public int size() {
return HashMap.this.size();
}
@Override
public void clear() {
HashMap.this.clear();
}
@Override
public Iterator< V > iterator() {
return new ValueIterator< K,V > (HashMap.this);
}
};
}
return valuesCollection;
}
Returns a collection of the values contained in this map. The collection
is backed by this map so changes to one are reflected by the other. The
collection supports remove, removeAll, retainAll and clear operations,
and it does not support add or addAll operations.
This method returns a collection which is the subclass of
AbstractCollection. The iterator method of this subclass returns a
"wrapper object" over the iterator of map's entrySet(). The {@code size}
method wraps the map's size method and the {@code contains} method wraps
the map's containsValue method.
The collection is created when this method is called for the first time
and returned in response to all subsequent calls. This method may return
different collections when multiple concurrent calls occur, since no
synchronization is performed. |