Created
March 20, 2015 02:11
-
-
Save ThomasLau/c7a2da2c4bcb11539b54 to your computer and use it in GitHub Desktop.
How HashMap serialize and deserialize itself and some awesome function in HashMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void writeObject(java.io.ObjectOutputStream s) | |
throws IOException { | |
int buckets = capacity(); | |
// Write out the threshold, loadfactor, and any hidden stuff | |
s.defaultWriteObject(); | |
s.writeInt(buckets); | |
s.writeInt(size); | |
internalWriteEntries(s); | |
} | |
/** | |
* Reconstitute the {@code HashMap} instance from a stream (i.e., | |
* deserialize it). | |
*/ | |
private void readObject(java.io.ObjectInputStream s) | |
throws IOException, ClassNotFoundException { | |
// Read in the threshold (ignored), loadfactor, and any hidden stuff | |
s.defaultReadObject(); | |
reinitialize(); | |
if (loadFactor <= 0 || Float.isNaN(loadFactor)) | |
throw new InvalidObjectException("Illegal load factor: " + | |
loadFactor); | |
s.readInt(); // Read and ignore number of buckets | |
int mappings = s.readInt(); // Read number of mappings (size) | |
if (mappings < 0) | |
throw new InvalidObjectException("Illegal mappings count: " + | |
mappings); | |
else if (mappings > 0) { // (if zero, use defaults) | |
// Size the table using given load factor only if within | |
// range of 0.25...4.0 | |
float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f); | |
float fc = (float)mappings / lf + 1.0f; | |
int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ? | |
DEFAULT_INITIAL_CAPACITY : | |
(fc >= MAXIMUM_CAPACITY) ? | |
MAXIMUM_CAPACITY : | |
tableSizeFor((int)fc)); | |
float ft = (float)cap * lf; | |
threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ? | |
(int)ft : Integer.MAX_VALUE); | |
@SuppressWarnings({"rawtypes","unchecked"}) | |
Node<K,V>[] tab = (Node<K,V>[])new Node[cap]; | |
table = tab; | |
// Read the keys and values, and put the mappings in the HashMap | |
for (int i = 0; i < mappings; i++) { | |
@SuppressWarnings("unchecked") | |
K key = (K) s.readObject(); | |
@SuppressWarnings("unchecked") | |
V value = (V) s.readObject(); | |
putVal(hash(key), key, value, false, false); | |
} | |
} | |
} | |
void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException { | |
Node<K,V>[] tab; | |
if (size > 0 && (tab = table) != null) { | |
for (int i = 0; i < tab.length; ++i) { | |
for (Node<K,V> e = tab[i]; e != null; e = e.next) { | |
s.writeObject(e.key); | |
s.writeObject(e.value); | |
} | |
} | |
} | |
} | |
public V computeIfPresent(K key, | |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | |
if (remappingFunction == null) | |
throw new NullPointerException(); | |
Node<K,V> e; V oldValue; | |
int hash = hash(key); | |
if ((e = getNode(hash, key)) != null && | |
(oldValue = e.value) != null) { | |
V v = remappingFunction.apply(key, oldValue); | |
if (v != null) { | |
e.value = v; | |
afterNodeAccess(e); | |
return v; | |
} | |
else | |
removeNode(hash, key, null, false, true); | |
} | |
return null; | |
} | |
@Override | |
public V compute(K key, | |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | |
if (remappingFunction == null) | |
throw new NullPointerException(); | |
int hash = hash(key); | |
Node<K,V>[] tab; Node<K,V> first; int n, i; | |
int binCount = 0; | |
TreeNode<K,V> t = null; | |
Node<K,V> old = null; | |
if (size > threshold || (tab = table) == null || | |
(n = tab.length) == 0) | |
n = (tab = resize()).length; | |
if ((first = tab[i = (n - 1) & hash]) != null) { | |
if (first instanceof TreeNode) | |
old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key); | |
else { | |
Node<K,V> e = first; K k; | |
do { | |
if (e.hash == hash && | |
((k = e.key) == key || (key != null && key.equals(k)))) { | |
old = e; | |
break; | |
} | |
++binCount; | |
} while ((e = e.next) != null); | |
} | |
} | |
V oldValue = (old == null) ? null : old.value; | |
V v = remappingFunction.apply(key, oldValue); | |
if (old != null) { | |
if (v != null) { | |
old.value = v; | |
afterNodeAccess(old); | |
} | |
else | |
removeNode(hash, key, null, false, true); | |
} | |
else if (v != null) { | |
if (t != null) | |
t.putTreeVal(this, tab, hash, key, v); | |
else { | |
tab[i] = newNode(hash, key, v, first); | |
if (binCount >= TREEIFY_THRESHOLD - 1) | |
treeifyBin(tab, hash); | |
} | |
++modCount; | |
++size; | |
afterNodeInsertion(true); | |
} | |
return v; | |
} | |
@Override | |
public V merge(K key, V value, | |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { | |
if (value == null) | |
throw new NullPointerException(); | |
if (remappingFunction == null) | |
throw new NullPointerException(); | |
int hash = hash(key); | |
Node<K,V>[] tab; Node<K,V> first; int n, i; | |
int binCount = 0; | |
TreeNode<K,V> t = null; | |
Node<K,V> old = null; | |
if (size > threshold || (tab = table) == null || | |
(n = tab.length) == 0) | |
n = (tab = resize()).length; | |
if ((first = tab[i = (n - 1) & hash]) != null) { | |
if (first instanceof TreeNode) | |
old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key); | |
else { | |
Node<K,V> e = first; K k; | |
do { | |
if (e.hash == hash && | |
((k = e.key) == key || (key != null && key.equals(k)))) { | |
old = e; | |
break; | |
} | |
++binCount; | |
} while ((e = e.next) != null); | |
} | |
} | |
if (old != null) { | |
V v; | |
if (old.value != null) | |
v = remappingFunction.apply(old.value, value); | |
else | |
v = value; | |
if (v != null) { | |
old.value = v; | |
afterNodeAccess(old); | |
} | |
else | |
removeNode(hash, key, null, false, true); | |
return v; | |
} | |
if (value != null) { | |
if (t != null) | |
t.putTreeVal(this, tab, hash, key, value); | |
else { | |
tab[i] = newNode(hash, key, value, first); | |
if (binCount >= TREEIFY_THRESHOLD - 1) | |
treeifyBin(tab, hash); | |
} | |
++modCount; | |
++size; | |
afterNodeInsertion(true); | |
} | |
return value; | |
} | |
@Override | |
public void forEach(BiConsumer<? super K, ? super V> action) { | |
Node<K,V>[] tab; | |
if (action == null) | |
throw new NullPointerException(); | |
if (size > 0 && (tab = table) != null) { | |
int mc = modCount; | |
for (int i = 0; i < tab.length; ++i) { | |
for (Node<K,V> e = tab[i]; e != null; e = e.next) | |
action.accept(e.key, e.value); | |
} | |
if (modCount != mc) | |
throw new ConcurrentModificationException(); | |
} | |
} | |
@Override | |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { | |
Node<K,V>[] tab; | |
if (function == null) | |
throw new NullPointerException(); | |
if (size > 0 && (tab = table) != null) { | |
int mc = modCount; | |
for (int i = 0; i < tab.length; ++i) { | |
for (Node<K,V> e = tab[i]; e != null; e = e.next) { | |
e.value = function.apply(e.key, e.value); | |
} | |
} | |
if (modCount != mc) | |
throw new ConcurrentModificationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment