jdk1.7之Hashtable



public class Hashtable<K, V> extends Dictionary<K, V> implements Map<K, V>, Cloneable, java.io.Serializable {


//存放hashtable数据
private transient Entry<K, V>[] table;


//hashtable大小
private transient int count;




private int threshold;


private float loadFactor;


private transient int modCount = 0;


/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1421746759512286392L;



static final int ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;




private static class Holder {


/**
* Table capacity above which to switch to use alternative hashing.
*/
static final int ALTERNATIVE_HASHING_THRESHOLD;


static {
String altThreshold = java.security.AccessController
.doPrivileged(new sun.security.action.GetPropertyAction("jdk.map.althashing.threshold"));


int threshold;
try {
threshold = (null != altThreshold) ? Integer.parseInt(altThreshold)
: ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;


// disable alternative hashing if -1
if (threshold == -1) {
threshold = Integer.MAX_VALUE;
}


if (threshold < 0) {
throw new IllegalArgumentException("value must be positive integer.");
}
} catch (IllegalArgumentException failed) {
throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
}


ALTERNATIVE_HASHING_THRESHOLD = threshold;
}
}



transient int hashSeed;


final boolean initHashSeedAsNeeded(int capacity) {
boolean currentAltHashing = hashSeed != 0;
boolean useAltHashing = sun.misc.VM.isBooted() && (capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
boolean switching = currentAltHashing ^ useAltHashing;
if (switching) {
hashSeed = useAltHashing ? sun.misc.Hashing.randomHashSeed(this) : 0;
}
return switching;
}


private int hash(Object k) {
// hashSeed will be zero if alternative hashing is disabled.
return hashSeed ^ k.hashCode();
}



public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: " + loadFactor);


if (initialCapacity == 0)
initialCapacity = 1;
this.loadFactor = loadFactor;
table = new Entry[initialCapacity];
threshold = (int) Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
initHashSeedAsNeeded(initialCapacity);
}



public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}



public Hashtable() {
this(11, 0.75f);
}



public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2 * t.size(), 11), 0.75f);
putAll(t);
}



public synchronized int size() {
return count;
}



public synchronized boolean isEmpty() {
return count == 0;
}



public synchronized Enumeration<K> keys() {
return this.<K> getEnumeration(KEYS);
}



public synchronized Enumeration<V> elements() {
return this.<V> getEnumeration(VALUES);
}



public synchronized boolean contains(Object value) {
if (value == null) {
throw new NullPointerException();
}


Entry tab[] = table;
for (int i = tab.length; i-- > 0;) {
for (Entry<K, V> e = tab[i]; e != null; e = e.next) {
if (e.value.equals(value)) {
return true;
}
}
}
return false;
}



public boolean containsValue(Object value) {
return contains(value);
}



public synchronized boolean containsKey(Object key) {
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return true;
}
}
return false;
}



public synchronized V get(Object key) {
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return e.value;
}
}
return null;
}



private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;


protected void rehash() {
int oldCapacity = table.length;
Entry<K, V>[] oldMap = table;


// overflow-conscious code
int newCapacity = (oldCapacity << 1) + 1;
if (newCapacity - MAX_ARRAY_SIZE > 0) {
if (oldCapacity == MAX_ARRAY_SIZE)
// Keep running with MAX_ARRAY_SIZE buckets
return;
newCapacity = MAX_ARRAY_SIZE;
}
Entry<K, V>[] newMap = new Entry[newCapacity];


modCount++;
threshold = (int) Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
boolean rehash = initHashSeedAsNeeded(newCapacity);


table = newMap;


for (int i = oldCapacity; i-- > 0;) {
for (Entry<K, V> old = oldMap[i]; old != null;) {
Entry<K, V> e = old;
old = old.next;


if (rehash) {
e.hash = hash(e.key);
}
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
}



public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}


// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}


modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();


tab = table;
hash = hash(key);
index = (hash & 0x7FFFFFFF) % tab.length;
}


// Creates the new entry.
Entry<K, V> e = tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
return null;
}



public synchronized V remove(Object key) {
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
modCount++;
if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
count--;
V oldValue = e.value;
e.value = null;
return oldValue;
}
}
return null;
}



public synchronized void putAll(Map<? extends K, ? extends V> t) {
for (Map.Entry<? extends K, ? extends V> e : t.entrySet())
put(e.getKey(), e.getValue());
}



public synchronized void clear() {
Entry tab[] = table;
modCount++;
for (int index = tab.length; --index >= 0;)
tab[index] = null;
count = 0;
}


public synchronized Object clone() {
try {
Hashtable<K, V> t = (Hashtable<K, V>) super.clone();
t.table = new Entry[table.length];
for (int i = table.length; i-- > 0;) {
t.table[i] = (table[i] != null) ? (Entry<K, V>) table[i].clone() : null;
}
t.keySet = null;
t.entrySet = null;
t.values = null;
t.modCount = 0;
return t;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}



public synchronized 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(", ");
}
}


private <T> Enumeration<T> getEnumeration(int type) {
if (count == 0) {
return Collections.emptyEnumeration();
} else {
return new Enumerator<>(type, false);
}
}


private <T> Iterator<T> getIterator(int type) {
if (count == 0) {
return Collections.emptyIterator();
} else {
return new Enumerator<>(type, true);
}
}


// Views



private transient volatile Set<K> keySet = null;
private transient volatile Set<Map.Entry<K, V>> entrySet = null;
private transient volatile Collection<V> values = null;




public Set<K> keySet() {
if (keySet == null)
keySet = Collections.synchronizedSet(new KeySet(), this);
return keySet;
}


private class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return getIterator(KEYS);
}


public int size() {
return count;
}


public boolean contains(Object o) {
return containsKey(o);
}


public boolean remove(Object o) {
return Hashtable.this.remove(o) != null;
}


public void clear() {
Hashtable.this.clear();
}
}




public Set<Map.Entry<K, V>> entrySet() {
if (entrySet == null)
entrySet = Collections.synchronizedSet(new EntrySet(), this);
return entrySet;
}


private class EntrySet extends AbstractSet<Map.Entry<K, V>> {
public Iterator<Map.Entry<K, V>> iterator() {
return getIterator(ENTRIES);
}


public boolean add(Map.Entry<K, V> o) {
return super.add(o);
}


public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry entry = (Map.Entry) o;
Object key = entry.getKey();
Entry[] tab = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;


for (Entry e = tab[index]; e != null; e = e.next)
if (e.hash == hash && e.equals(entry))
return true;
return false;
}


public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K, V> entry = (Map.Entry<K, V>) o;
K key = entry.getKey();
Entry[] tab = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;


for (Entry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
if (e.hash == hash && e.equals(entry)) {
modCount++;
if (prev != null)
prev.next = e.next;
else
tab[index] = e.next;


count--;
e.value = null;
return true;
}
}
return false;
}


public int size() {
return count;
}


public void clear() {
Hashtable.this.clear();
}
}



public Collection<V> values() {
if (values == null)
values = Collections.synchronizedCollection(new ValueCollection(), this);
return values;
}


private class ValueCollection extends AbstractCollection<V> {
public Iterator<V> iterator() {
return getIterator(VALUES);
}


public int size() {
return count;
}


public boolean contains(Object o) {
return containsValue(o);
}


public void clear() {
Hashtable.this.clear();
}
}


// Comparison and hashing




public synchronized boolean equals(Object o) {
if (o == this)
return true;


if (!(o instanceof Map))
return false;
Map<K, V> t = (Map<K, V>) o;
if (t.size() != size())
return false;


try {
Iterator<Map.Entry<K, V>> i = entrySet().iterator();
while (i.hasNext()) {
Map.Entry<K, V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(t.get(key) == null && t.containsKey(key)))
return false;
} else {
if (!value.equals(t.get(key)))
return false;
}
}
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}


return true;
}


public synchronized int hashCode() {
/*
* This code detects the recursion caused by computing the hash code of
* a self-referential hash table and prevents the stack overflow that
* would otherwise result. This allows certain 1.1-era applets with
* self-referential hash tables to work. This code abuses the loadFactor
* field to do double-duty as a hashCode in progress flag, so as not to
* worsen the space performance. A negative load factor indicates that
* hash code computation is in progress.
*/
int h = 0;
if (count == 0 || loadFactor < 0)
return h; // Returns zero


loadFactor = -loadFactor; // Mark hashCode computation in progress
Entry[] tab = table;
for (Entry<K, V> entry : tab)
while (entry != null) {
h += entry.hashCode();
entry = entry.next;
}
loadFactor = -loadFactor; // Mark hashCode computation complete


return h;
}



private void writeObject(java.io.ObjectOutputStream s) throws IOException {
Entry<K, V> entryStack = null;


synchronized (this) {
// Write out the length, threshold, loadfactor
s.defaultWriteObject();


// Write out length, count of elements
s.writeInt(table.length);
s.writeInt(count);


// Stack copies of the entries in the table
for (int index = 0; index < table.length; index++) {
Entry<K, V> entry = table[index];


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;
}
}


private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
// Read in the length, threshold, and loadfactor
s.defaultReadObject();


// Read the original length of the array and number of elements
int origlength = s.readInt();
int elements = s.readInt();


// Compute new size with a bit of room 5% to grow but
// no larger than the original size. 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 * loadFactor) + (elements / 20) + 3;
if (length > elements && (length & 1) == 0)
length--;
if (origlength > 0 && length > origlength)
length = origlength;


Entry<K, V>[] newTable = new Entry[length];
threshold = (int) Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
count = 0;
initHashSeedAsNeeded(length);


// Read the number of elements and then all the key/value objects
for (; elements > 0; elements--) {
K key = (K) s.readObject();
V value = (V) s.readObject();
// synch could be eliminated for performance
reconstitutionPut(newTable, key, value);
}
this.table = newTable;
}



private void reconstitutionPut(Entry<K, V>[] tab, K key, V value) throws StreamCorruptedException {
if (value == null) {
throw new java.io.StreamCorruptedException();
}
// Makes sure the key is not already in the hashtable.
// This should not happen in deserialized version.
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
throw new java.io.StreamCorruptedException();
}
}
// Creates the new entry.
Entry<K, V> e = tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}


private static class Entry<K, V> implements Map.Entry<K, V> {
int hash;
final K key;
V value;
Entry<K, V> next;


protected Entry(int hash, K key, V value, Entry<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}


protected Object clone() {
return new Entry<>(hash, key, value, (next == null ? null : (Entry<K, V>) next.clone()));
}


// Map.Entry Ops


public K getKey() {
return key;
}


public V getValue() {
return value;
}


public V setValue(V value) {
if (value == null)
throw new NullPointerException();


V oldValue = this.value;
this.value = value;
return oldValue;
}


public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?, ?> e = (Map.Entry) o;


return key.equals(e.getKey()) && value.equals(e.getValue());
}


public int hashCode() {
return (Objects.hashCode(key) ^ Objects.hashCode(value));
}


public String toString() {
return key.toString() + "=" + value.toString();
}
}


// Types of Enumerations/Iterations
private static final int KEYS = 0;
private static final int VALUES = 1;
private static final int ENTRIES = 2;


private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
Entry[] table = Hashtable.this.table;
int index = table.length;
Entry<K, V> entry = null;
Entry<K, V> lastReturned = null;
int type;


boolean iterator;


protected int expectedModCount = modCount;


Enumerator(int type, boolean iterator) {
this.type = type;
this.iterator = iterator;
}


public boolean hasMoreElements() {
Entry<K, V> 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;
}


public T nextElement() {
Entry<K, V> 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<K, V> e = lastReturned = entry;
entry = e.next;
return type == KEYS ? (T) e.key : (type == VALUES ? (T) e.value : (T) e);
}
throw new NoSuchElementException("Hashtable Enumerator");
}


// Iterator methods
public boolean hasNext() {
return hasMoreElements();
}


public T next() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return nextElement();
}


public void remove() {
if (!iterator)
throw new UnsupportedOperationException();
if (lastReturned == null)
throw new IllegalStateException("Hashtable Enumerator");
if (modCount != expectedModCount)
throw new ConcurrentModificationException();


synchronized (Hashtable.this) {
Entry[] tab = Hashtable.this.table;
int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;


for (Entry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
if (e == lastReturned) {
modCount++;
expectedModCount++;
if (prev == null)
tab[index] = e.next;
else
prev.next = e.next;
count--;
lastReturned = null;
return;
}
}
throw new ConcurrentModificationException();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值