for (int i = oldCapacity ; i-- > 0 ;) { for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
Entry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = (Entry<K,V>)newMap[index];
newMap[index] = e;
}
}
}
privatevoid addEntry(int hash, K key, V value, int index) {
Entry<?,?> tab[] = table; if (count >= threshold) { // Rehash the table if the threshold is exceeded
rehash();
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
modCount++;
}
/** *Mapsthespecified{@codekey}tothespecified *{@codevalue}inthishashtable.Neitherthekeynorthe *valuecanbe{@codenull}.<p> * *Thevaluecanberetrievedbycallingthe{@codeget}method *withakeythatisequaltotheoriginalkey. * *@paramkeythehashtablekey *@paramvaluethevalue *@returnthepreviousvalueofthespecifiedkeyinthishashtable, *or{@codenull}ifitdidnothaveone *@throwsNullPointerExceptionifthekeyorvalueis *{@codenull} *@seeObject#equals(Object) *@see#get(Object)
*/ publicsynchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { thrownew NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value; return old;
}
}
addEntry(hash, key, value, index); returnnull;
}
/** *Removesthekey(anditscorrespondingvalue)fromthis *hashtable.Thismethoddoesnothingifthekeyisnotinthehashtable. * *@paramkeythekeythatneedstoberemoved *@returnthevaluetowhichthekeyhadbeenmappedinthishashtable, *or{@codenull}ifthekeydidnothaveamapping *@throwsNullPointerExceptionifthekeyis{@codenull}
*/ publicsynchronized V remove(Object key) {
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
modCount++;
count--;
V oldValue = e.value;
e.value = null; return oldValue;
}
} returnnull;
}
/** Calls super.clone() */ final Hashtable<?,?> cloneHashtable() { try { return (Hashtable<?,?>)super.clone();
} catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable thrownew InternalError(e);
}
}
/** *Returnsastringrepresentationofthis{@codeHashtable}object *intheformofasetofentries,enclosedinbracesandseparated *bytheASCIIcharacters"<code>, </code>"(commaandspace).Each *entryisrenderedasthekey,anequalssign{@code=},andthe *associatedelement,wherethe{@codetoString}methodisusedto *convertthekeyandelementtostrings. * *@returnastringrepresentationofthishashtable
*/ publicsynchronized String toString() { int max = size() - 1; if (max == -1) return"{}";
StringBuilder sb = new StringBuilder();
Iterator<Map.Entry<K,V>> it = entrySet().iterator();
sb.append('{'); for (int i = 0; ; i++) {
Map.Entry<K,V> e = it.next();
K key = e.getKey();
V value = e.getValue();
sb.append(key == this ? "(this Map)" : key.toString());
sb.append('=');
sb.append(value == this ? "(this Map)" : value.toString());
if (i == max) return sb.append('}').toString();
sb.append(", ");
}
}
@Override publicsynchronized V putIfAbsent(K key, V value) {
Objects.requireNonNull(value);
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index]; for (; entry != null; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value; if (old == null) {
entry.value = value;
} return old;
}
}
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for (; e != null; e = e.next) { if (e.hash == hash && e.key.equals(key)) { // Hashtable not accept null value return e.value;
}
}
int mc = modCount;
V newValue = mappingFunction.apply(key); if (mc != modCount) { thrownew ConcurrentModificationException(); } if (newValue != null) {
addEntry(hash, key, newValue, index);
}
return newValue;
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkjava.util.ConcurrentModificationException}iftheremapping *functionmodifiedthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override publicsynchronized V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) { if (e.hash == hash && e.key.equals(key)) { int mc = modCount;
V newValue = remappingFunction.apply(key, e.value); if (mc != modCount) { thrownew ConcurrentModificationException();
} if (newValue == null) { if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
modCount = mc + 1;
count--;
} else {
e.value = newValue;
} return newValue;
}
} returnnull;
} /** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkjava.util.ConcurrentModificationException}iftheremapping *functionmodifiedthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override publicsynchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) { if (e.hash == hash && Objects.equals(e.key, key)) { int mc = modCount;
V newValue = remappingFunction.apply(key, e.value); if (mc != modCount) { thrownew ConcurrentModificationException();
} if (newValue == null) { if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
modCount = mc + 1;
count--;
} else {
e.value = newValue;
} return newValue;
}
}
int mc = modCount;
V newValue = remappingFunction.apply(key, null); if (mc != modCount) { thrownew ConcurrentModificationException(); } if (newValue != null) {
addEntry(hash, key, newValue, index);
}
return newValue;
}
/** *{@inheritDoc} * *<p>Thismethodwill,onabest-effortbasis,throwa *{@linkjava.util.ConcurrentModificationException}iftheremapping *functionmodifiedthismapduringcomputation. * *@throwsConcurrentModificationExceptionifitisdetectedthatthe *remappingfunctionmodifiedthismap
*/
@Override publicsynchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) { if (e.hash == hash && e.key.equals(key)) { int mc = modCount;
V newValue = remappingFunction.apply(e.value, value); if (mc != modCount) { thrownew ConcurrentModificationException();
} if (newValue == null) { if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
modCount = mc + 1;
count--;
} else {
e.value = newValue;
} return newValue;
}
}
if (value != null) {
addEntry(hash, key, value, index);
}
synchronized (this) { // Write out the threshold and loadFactor
s.defaultWriteObject();
// Write out the length and count of elements
s.writeInt(table.length);
s.writeInt(count);
// Stack copies of the entries in the table for (Entry<?, ?> entry : table) {
while (entry != null) {
entryStack = new Entry<>(0, entry.key, entry.value, entryStack);
entry = entry.next;
}
}
}
// Write out the key/value objects from the stacked entries while (entryStack != null) {
s.writeObject(entryStack.key);
s.writeObject(entryStack.value);
entryStack = entryStack.next;
}
}
// Read and validate loadFactor (ignore threshold - it will be re-computed) float lf = fields.get("loadFactor", 0.75f); if (lf <= 0 || Float.isNaN(lf)) thrownew StreamCorruptedException("Illegal load factor: " + lf);
lf = Math.min(Math.max(0.25f, lf), 4.0f);
// Read the original length of the array and number of elements int origlength = s.readInt(); int elements = s.readInt();
// Validate # of elements if (elements < 0) thrownew StreamCorruptedException("Illegal # of Elements: " + elements);
// Clamp original length to be more than elements / loadFactor // (this is the invariant enforced with auto-growth)
origlength = Math.max(origlength, (int)(elements / lf) + 1);
// Compute new length with a bit of room 5% + 3 to grow but // no larger than the clamped original length. Make the length // odd if it's large enough, this helps distribute the entries. // Guard against the length ending up zero, that's not valid. int length = (int)(elements * 1.05f / lf) + 3; if (length > elements && (length & 1) == 0)
length--;
length = Math.min(length, origlength);
// Check Map.Entry[].class since it's the nearest public type to // what we're actually creating.
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Map.Entry[].class, length);
Hashtable.UnsafeHolder.putLoadFactor(this, lf);
table = new Entry<?,?>[length];
threshold = (int)Math.min(length * lf, MAX_ARRAY_SIZE + 1);
count = 0;
// Read the number of elements and then all the key/value objects for (; elements > 0; elements--) {
@SuppressWarnings("unchecked")
K key = (K)s.readObject();
@SuppressWarnings("unchecked")
V value = (V)s.readObject(); // sync is eliminated for performance
reconstitutionPut(table, key, value);
}
}
// Support for resetting final field during deserializing privatestaticfinalclass UnsafeHolder { private UnsafeHolder() { thrownew InternalError(); } privatestaticfinal jdk.internal.misc.Unsafe unsafe
= jdk.internal.misc.Unsafe.getUnsafe(); privatestaticfinallong LF_OFFSET
= unsafe.objectFieldOffset(Hashtable.class, "loadFactor"); staticvoid putLoadFactor(Hashtable<?, ?> table, float lf) {
unsafe.putFloat(table, LF_OFFSET, lf);
}
}
/** *TheputmethodusedbyreadObject.Thisisprovidedbecauseput *isoverridableandshouldnotbecalledinreadObjectsincethe *subclasswillnotyetbeinitialized. * *<p>Thisdiffersfromtheregularputmethodinseveralways.No *checkingforrehashingisnecessarysincethenumberofelements *initiallyinthetableisknown.ThemodCountisnotincrementedand *there'snosynchronizationbecausewearecreatinganewinstance. *Also,noreturnvalueisneeded.
*/ privatevoid reconstitutionPut(Entry<?,?>[] tab, K key, V value) throws StreamCorruptedException
{ if (value == null) { thrownew java.io.StreamCorruptedException();
} // Makes sure the key is not already in the hashtable. // This should not happen in deserialized version. int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { thrownew java.io.StreamCorruptedException();
}
} // Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
/** *Hashtablebucketcollisionlistentry
*/ privatestaticclass Entry<K,V> implements Map.Entry<K,V> { finalint hash; final K key;
V value;
Entry<K,V> next;
publicboolean hasMoreElements() {
Entry<?,?> e = entry; int i = index;
Entry<?,?>[] t = table; /* Use locals for faster loop iteration */ while (e == null && i > 0) {
e = t[--i];
}
entry = e;
index = i; return e != null;
}
@SuppressWarnings("unchecked") public T nextElement() {
Entry<?,?> et = entry; int i = index;
Entry<?,?>[] t = table; /* Use locals for faster loop iteration */ while (et == null && i > 0) {
et = t[--i];
}
entry = et;
index = i; if (et != null) {
Entry<?,?> e = lastReturned = entry;
entry = e.next; return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e);
} thrownew NoSuchElementException("Hashtable Enumerator");
}
public T next() { if (Hashtable.this.modCount != expectedModCount) thrownew ConcurrentModificationException(); return nextElement();
}
publicvoid remove() { if (!iterator) thrownew UnsupportedOperationException(); if (lastReturned == null) thrownew IllegalStateException("Hashtable Enumerator"); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
synchronized(Hashtable.this) {
Entry<?,?>[] tab = Hashtable.this.table; int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index]; for(Entry<K,V> prev = null; e != null; prev = e, e = e.next) { if (e == lastReturned) { if (prev == null)
tab[index] = e.next; else
prev.next = e.next;
expectedModCount++;
lastReturned = null;
Hashtable.this.modCount++;
Hashtable.this.count--; return;
}
} thrownew ConcurrentModificationException();
}
}
}
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.60Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-10-06)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.