Methods Summary |
---|
public synchronized void | clear()Removes all key/value pairs from this {@code Hashtable}, leaving the
size zero and the capacity unchanged.
elementCount = 0;
Arrays.fill(elementData, null);
modCount++;
|
public synchronized java.lang.Object | clone()Returns a new {@code Hashtable} with the same key/value pairs, capacity
and load factor.
try {
Hashtable<K, V> hashtable = (Hashtable<K, V>) super.clone();
hashtable.elementData = elementData.clone();
Entry<K, V> entry;
for (int i = elementData.length; --i >= 0;) {
if ((entry = elementData[i]) != null) {
hashtable.elementData[i] = (Entry<K, V>) entry.clone();
}
}
return hashtable;
} catch (CloneNotSupportedException e) {
return null;
}
|
private void | computeMaxSize()
threshold = (int) (elementData.length * loadFactor);
|
public synchronized boolean | contains(java.lang.Object value)Returns true if this {@code Hashtable} contains the specified object as
the value of at least one of the key/value pairs.
if (value == null) {
throw new NullPointerException();
}
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;
}
}
return false;
|
public synchronized boolean | containsKey(java.lang.Object key)Returns true if this {@code Hashtable} contains the specified object as a
key of one of the key/value pairs.
return getEntry(key) != null;
|
public boolean | containsValue(java.lang.Object value)Searches this {@code Hashtable} for the specified value.
return contains(value);
|
public synchronized java.util.Enumeration | elements()Returns an enumeration on the values of this {@code Hashtable}. The
results of the Enumeration may be affected if the contents of this
{@code Hashtable} are modified.
if (elementCount == 0) {
return (Enumeration<V>) EMPTY_ENUMERATION;
}
return new HashEnumerator<V>(false);
|
public java.util.Set | entrySet()Returns a set of the mappings contained in this {@code Hashtable}. Each
element in the set is a {@link Map.Entry}. The set is backed by this
{@code Hashtable} so changes to one are reflected by the other. The set
does not support adding.
return new Collections.SynchronizedSet<Map.Entry<K, V>>(
new AbstractSet<Map.Entry<K, V>>() {
@Override
public int size() {
synchronized (Hashtable.this) {
return elementCount;
}
}
@Override
public void clear() {
Hashtable.this.clear();
}
@Override
@SuppressWarnings("unchecked")
public boolean remove(Object object) {
synchronized (Hashtable.this) {
if (contains(object)) {
Hashtable.this
.remove(((Map.Entry<K, V>) object)
.getKey());
return true;
}
return false;
}
}
@Override
@SuppressWarnings("unchecked")
public boolean contains(Object object) {
synchronized (Hashtable.this) {
Entry<K, V> entry = getEntry(((Map.Entry<K, V>) object)
.getKey());
return object.equals(entry);
}
}
@Override
public Iterator<Map.Entry<K, V>> iterator() {
return new HashIterator<Map.Entry<K, V>>(
new MapEntry.Type<Map.Entry<K, V>, K, V>() {
public Map.Entry<K, V> get(
MapEntry<K, V> entry) {
return entry;
}
});
}
}, this);
|
public synchronized boolean | equals(java.lang.Object object)Compares this {@code Hashtable} with the specified object and indicates
if they are equal. In order to be equal, {@code object} must be an
instance of Map and contain the same key/value pairs.
if (this == object) {
return true;
}
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
if (size() != map.size()) {
return false;
}
Set<Map.Entry<K, V>> entries = entrySet();
for (Map.Entry<?, ?> e : map.entrySet()) {
if (!entries.contains(e)) {
return false;
}
}
return true;
}
return false;
|
public synchronized V | get(java.lang.Object key)Returns the value associated with the specified key in this
{@code Hashtable}.
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
if (entry.equalsKey(key, hash)) {
return entry.value;
}
entry = entry.next;
}
return null;
|
java.util.Hashtable$Entry | getEntry(java.lang.Object key)
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
if (entry.equalsKey(key, hash)) {
return entry;
}
entry = entry.next;
}
return null;
|
public synchronized int | hashCode()
int result = 0;
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
Object key = entry.getKey();
Object value = entry.getValue();
int hash = (key != this ? key.hashCode() : 0)
^ (value != this ? (value != null ? value.hashCode() : 0)
: 0);
result += hash;
}
return result;
|
public synchronized boolean | isEmpty()Returns true if this {@code Hashtable} has no key/value pairs.
return elementCount == 0;
|
public java.util.Set | keySet()Returns a set of the keys contained in this {@code Hashtable}. The set
is backed by this {@code Hashtable} so changes to one are reflected by
the other. The set does not support adding.
return new Collections.SynchronizedSet<K>(new AbstractSet<K>() {
@Override
public boolean contains(Object object) {
synchronized (Hashtable.this) {
return containsKey(object);
}
}
@Override
public int size() {
synchronized (Hashtable.this) {
return elementCount;
}
}
@Override
public void clear() {
Hashtable.this.clear();
}
@Override
public boolean remove(Object key) {
synchronized (Hashtable.this) {
if (containsKey(key)) {
Hashtable.this.remove(key);
return true;
}
return false;
}
}
@Override
public Iterator<K> iterator() {
return new HashIterator<K>(new MapEntry.Type<K, K, V>() {
public K get(MapEntry<K, V> entry) {
return entry.key;
}
});
}
}, this);
|
public synchronized java.util.Enumeration | keys()Returns an enumeration on the keys of this {@code Hashtable} instance.
The results of the enumeration may be affected if the contents of this
{@code Hashtable} are modified.
if (elementCount == 0) {
return (Enumeration<K>) EMPTY_ENUMERATION;
}
return new HashEnumerator<K>(true);
|
private java.util.Hashtable$Entry[] | newElementArray(int size)
return new Entry[size];
|
private static java.util.Hashtable$Entry | newEntry(K key, V value, int hash)
return new Entry<K, V>(key, value);
|
public synchronized V | put(K key, V value)Associate the specified value with the specified key in this
{@code Hashtable}. If the key already exists, the old value is replaced.
The key and value cannot be null.
if (key != null && value != null) {
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null && !entry.equalsKey(key, hash)) {
entry = entry.next;
}
if (entry == null) {
modCount++;
if (++elementCount > threshold) {
rehash();
index = (hash & 0x7FFFFFFF) % elementData.length;
}
if (index < firstSlot) {
firstSlot = index;
}
if (index > lastSlot) {
lastSlot = index;
}
entry = newEntry(key, value, hash);
entry.next = elementData[index];
elementData[index] = entry;
return null;
}
V result = entry.value;
entry.value = value;
return result;
}
throw new NullPointerException();
|
public synchronized void | putAll(java.util.Map map)Copies every mapping to this {@code Hashtable} from the specified map.
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
|
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;) {
Object key = stream.readObject();
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % length;
if (index < firstSlot) {
firstSlot = index;
}
if (index > lastSlot) {
lastSlot = index;
}
Entry<K, V> entry = newEntry((K) key, (V) stream.readObject(), hash);
entry.next = elementData[index];
elementData[index] = entry;
}
|
protected void | rehash()Increases the capacity of this {@code Hashtable}. This method is called
when the size of this {@code Hashtable} exceeds the load factor.
int length = (elementData.length << 1) + 1;
if (length == 0) {
length = 1;
}
int newFirst = length;
int newLast = -1;
Entry<K, V>[] newData = newElementArray(length);
for (int i = lastSlot + 1; --i >= firstSlot;) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
int index = (entry.getKeyHash() & 0x7FFFFFFF) % length;
if (index < newFirst) {
newFirst = index;
}
if (index > newLast) {
newLast = index;
}
Entry<K, V> next = entry.next;
entry.next = newData[index];
newData[index] = entry;
entry = next;
}
}
firstSlot = newFirst;
lastSlot = newLast;
elementData = newData;
computeMaxSize();
|
public synchronized V | remove(java.lang.Object key)Removes the key/value pair with the specified key from this
{@code Hashtable}.
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % elementData.length;
Entry<K, V> last = null;
Entry<K, V> entry = elementData[index];
while (entry != null && !entry.equalsKey(key, hash)) {
last = entry;
entry = entry.next;
}
if (entry != null) {
modCount++;
if (last == null) {
elementData[index] = entry.next;
} else {
last.next = entry.next;
}
elementCount--;
V result = entry.value;
entry.value = null;
return result;
}
return null;
|
public synchronized int | size()Returns the number of key/value pairs in this {@code Hashtable}.
return elementCount;
|
public synchronized java.lang.String | toString()Returns the string representation of this {@code Hashtable}.
if (isEmpty()) {
return "{}"; //$NON-NLS-1$
}
StringBuilder buffer = new StringBuilder(size() * 28);
buffer.append('{");
for (int i = lastSlot; i >= firstSlot; i--) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
if (entry.key != this) {
buffer.append(entry.key);
} else {
// luni.04=this Map
buffer.append("(" + Messages.getString("luni.04") + ")"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
buffer.append('=");
if (entry.value != this) {
buffer.append(entry.value);
} else {
// luni.04=this Map
buffer.append("(" + Messages.getString("luni.04") + ")"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
buffer.append(", "); //$NON-NLS-1$
entry = entry.next;
}
}
// Remove the last ", "
if (elementCount > 0) {
buffer.setLength(buffer.length() - 2);
}
buffer.append('}");
return buffer.toString();
|
public java.util.Collection | values()Returns a collection of the values contained in this {@code Hashtable}.
The collection is backed by this {@code Hashtable} so changes to one are
reflected by the other. The collection does not support adding.
return new Collections.SynchronizedCollection<V>(
new AbstractCollection<V>() {
@Override
public boolean contains(Object object) {
synchronized (Hashtable.this) {
return Hashtable.this.contains(object);
}
}
@Override
public int size() {
synchronized (Hashtable.this) {
return elementCount;
}
}
@Override
public void clear() {
Hashtable.this.clear();
}
@Override
public Iterator<V> iterator() {
return new HashIterator<V>(
new MapEntry.Type<V, K, V>() {
public V get(MapEntry<K, V> entry) {
return entry.value;
}
});
}
}, this);
|
private synchronized void | writeObject(java.io.ObjectOutputStream stream)
stream.defaultWriteObject();
stream.writeInt(elementData.length);
stream.writeInt(elementCount);
for (int i = elementData.length; --i >= 0;) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
stream.writeObject(entry.key);
stream.writeObject(entry.value);
entry = entry.next;
}
}
|