手寫hashMap的簡單實現
public class MyHashMap<K, V> {
private static final int DEFAULT_CAPITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private Entry<K, V>[] table;
private int capity;
private float loadFactor;
private int size;
private int threshold;
public MyHashMap() {
this.capity = DEFAULT_CAPITY;
this.loadFactor = DEFAULT_LOAD_FACTOR;
this.threshold = (int) (DEFAULT_CAPITY * DEFAULT_LOAD_FACTOR);
}
public MyHashMap(int capity) {
this.capity = tableSizeFor(capity);
this.loadFactor = DEFAULT_LOAD_FACTOR;
this.threshold = (int) (this.capity * this.loadFactor);
}
public void put (K key, V value) {
if (null == table) {
table = new Entry[capity];
}
int hashCode = hash(key);
int index = indexFor(hashCode);
for (Entry<K, V> entry = table[index]; entry != null; entry = entry.next) {
if (entry.key.equals(key)) {
entry.value = value;
return;
}
}
addEntry(hashCode, key, value, index);
}
public V get(K key) {
int hashCode = hash(key);
int index = indexFor(hashCode);
for (Entry<K, V> entry = table[index]; entry != null; entry = entry.next) {
if (entry.key.equals(key)) {
return entry.value;
}
}
return null;
}
private void addEntry(int hash, K key, V value, int index) {
Entry<K, V> entry = new Entry<>(key, value, hash, table[index]);
table[index] = entry;
++size;
if (size > threshold) {
resize();
}
}
private void resize() {
Entry<K, V>[] oldTable = table;
int oldCap = oldTable.length;
capity = capity << 1;
threshold = threshold << 1;
table = new Entry[capity];
for (int i = 0; i < oldCap; ++i) {
Entry<K, V> entry;
if ((entry = oldTable[i]).next == null) {
table[entry.hash & (capity - 1)] = entry;
} else {
Entry<K, V> loHead = null, loTail = null;
Entry<K, V> hiHead = null, hiTail = null;
Entry<K, V> next;
do {
next = entry.next;
if ((entry.hash & oldCap) == 0) {
if (null == loHead) {
loHead = entry;
} else {
loTail.next = entry;
}
loTail = entry;
} else {
if (null == hiHead) {
hiHead = entry;
} else {
hiTail.next = entry;
}
hiTail = entry;
}
} while ((entry = next) != null);
if (null != loTail) {
table[i] = loHead;
}
if (null != hiTail) {
table[i + oldCap] = hiHead;
}
}
}
}
private int indexFor(int hashCode) {
return hashCode & (capity - 1);
}
private int hash(K key) {
if (null == key) {
return 0;
}
int h = key.hashCode();
return h ^ (h >> 16);
}
private int tableSizeFor(int capity) {
int n = capity - 1;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n < 0 ? DEFAULT_CAPITY : (n + 1);
}
class Entry<K, V> {
private K key;
private V value;
private int hash;
private Entry<K, V> next;
public Entry(K key, V value, int hash, Entry<K, V> next) {
this.key = key;
this.value = value;
this.hash = hash;
this.next = next;
}
}
public static void main(String[] args) {
MyHashMap<String, String> myhashMap = new MyHashMap<>(4);
myhashMap.put("1", "1v");
myhashMap.put("2", "2v");
myhashMap.put("3", "3v");
myhashMap.put("4", "4v");
myhashMap.put("5", "5v");
myhashMap.put("5", "6v");
System.out.println(myhashMap.get("5"));
System.out.println("capity : " + myhashMap.capity);
System.out.println("size : " + myhashMap.size);
}
}