Methods Summary |
---|
public void | clear()Removes all mappings from this hash map, leaving it empty.
internalClear();
|
public java.lang.Object | clone()Returns a shallow copy of this map.
try {
// BEGIN android-changed
// copied from newer version of harmony
HashMap<K, V> map = (HashMap<K, V>) super.clone();
map.elementData = newElementArray(elementData.length);
map.internalClear();
Entry<K, V> entry;
for (int i = 0; i < elementData.length; i++) {
if ((entry = elementData[i]) != null){
map.putImpl(entry.getKey(), entry.getValue());
while (entry.next != null){
entry = entry.next;
map.putImpl(entry.getKey(), entry.getValue());
}
}
// END android-changed
}
return map;
} catch (CloneNotSupportedException e) {
return null;
}
|
private void | computeMaxSize()
threshold = (int) (elementData.length * loadFactor);
|
public boolean | containsKey(java.lang.Object key)Returns whether this map contains the specified key.
Entry<K, V> m;
if (key == null) {
m = findNullKeyEntry();
} else {
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
m = findNonNullKeyEntry(key, index, hash);
}
return m != null;
|
public boolean | containsValue(java.lang.Object value)Returns whether this map contains the specified value.
if (value != null) {
for (int i = elementData.length; --i >= 0;) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
if (value.equals(entry.value)) {
return true;
}
entry = entry.next;
}
}
} else {
for (int i = elementData.length; --i >= 0;) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
if (entry.value == null) {
return true;
}
entry = entry.next;
}
}
}
return false;
|
java.util.HashMap$Entry | 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;
|
java.util.HashMap$Entry | 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 java.util.Set | entrySet()Returns a set containing all of the mappings in this map. Each mapping is
an instance of {@link Map.Entry}. As the set is backed by this map,
changes in one will be reflected in the other.
return new HashMapEntrySet<K, V>(this);
|
final java.util.HashMap$Entry | findNonNullKeyEntry(java.lang.Object key, int index, int keyHash)
Entry<K,V> m = elementData[index];
// BEGIN android-changed
// The VM can optimize String.equals but not Object.equals
if (key instanceof String) {
String keyString = (String) key;
while (m != null) {
if (m.origKeyHash == keyHash) {
if (keyString.equals(m.key)) {
return m;
}
}
m = m.next;
}
} else {
while (m != null) {
if (m.origKeyHash == keyHash) {
if (key.equals(m.key)) {
return m;
}
}
m = m.next;
}
}
return null;
// END android-changed
|
final java.util.HashMap$Entry | findNullKeyEntry()
Entry<K,V> m = elementData[0];
while (m != null && m.key != null)
m = m.next;
return m;
|
public V | get(java.lang.Object key)Returns the value of the mapping with the specified key.
Entry<K, V> m;
if (key == null) {
m = findNullKeyEntry();
} else {
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
m = findNonNullKeyEntry(key, index, hash);
}
if (m != null) {
return m.value;
}
return null;
|
void | internalClear()
if (elementCount > 0) {
elementCount = 0;
Arrays.fill(elementData, null);
modCount++;
}
|
public boolean | isEmpty()Returns whether this map is empty.
return elementCount == 0;
|
public java.util.Set | 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.
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 HashMapIterator<K, K, V>(
new MapEntry.Type<K, K, V>() {
public K get(MapEntry<K, V> entry) {
return entry.key;
}
}, HashMap.this);
}
};
}
return keySet;
|
java.util.HashMap$Entry[] | newElementArray(int s)
return new Entry[s];
|
public V | put(K key, V value)Maps the specified key to the specified value.
return putImpl(key, value);
|
public void | putAll(java.util.Map 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.
if (!map.isEmpty()) {
putAllImpl(map);
}
|
private void | putAllImpl(java.util.Map map)
int capacity = elementCount + map.size();
if (capacity > threshold) {
rehash(capacity);
}
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
putImpl(entry.getKey(), entry.getValue());
}
|
private 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(key, 0, 0);
}
} else {
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
entry = findNonNullKeyEntry(key, index, hash);
if (entry == null) {
modCount++;
if (++elementCount > threshold) {
rehash();
index = (hash & 0x7FFFFFFF) % elementData.length;
}
entry = createHashedEntry(key, index, hash);
}
}
V result = entry.value;
entry.value = value;
return result;
|
private void | readObject(java.io.ObjectInputStream stream)
stream.defaultReadObject();
int length = stream.readInt();
elementData = newElementArray(length);
elementCount = stream.readInt();
for (int i = elementCount; --i >= 0;) {
K key = (K) stream.readObject();
int index = (null == key) ? 0 : (key.hashCode() & 0x7FFFFFFF)
% length;
createEntry(key, index, (V) stream.readObject());
}
|
void | rehash(int capacity)
int length = (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 & 0x7FFFFFFF) % length;
Entry<K, V> next = entry.next;
entry.next = newData[index];
newData[index] = entry;
entry = next;
}
}
elementData = newData;
computeMaxSize();
|
void | rehash()
rehash(elementData.length);
|
public V | remove(java.lang.Object key)Removes the mapping with the specified key from this map.
Entry<K, V> entry = removeEntry(key);
if (entry != null) {
return entry.value;
}
return null;
|
java.util.HashMap$Entry | removeEntry(java.lang.Object key)
int index = 0;
Entry<K, V> entry;
Entry<K, V> last = null;
if (key != null) {
int hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % elementData.length;
entry = elementData[index];
while (entry != null && !(entry.origKeyHash == hash && key.equals(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()Returns the number of elements in this map.
return elementCount;
|
public java.util.Collection | values()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.
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 HashMapIterator<V, K, V>(
new MapEntry.Type<V, K, V>() {
public V get(MapEntry<K, V> entry) {
return entry.value;
}
}, HashMap.this);
}
};
}
return valuesCollection;
|
private void | writeObject(java.io.ObjectOutputStream stream)
stream.defaultWriteObject();
stream.writeInt(elementData.length);
stream.writeInt(elementCount);
Iterator<?> iterator = entrySet().iterator();
while (iterator.hasNext()) {
Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
stream.writeObject(entry.key);
stream.writeObject(entry.value);
entry = entry.next;
}
|