HashMap代码实现
时间: 2023-05-16 17:07:15 浏览: 64
HashMap 是一种常用的数据结构,它可以用来存储键值对,通过键来快速查找对应的值。以下是一个简单的 HashMap 实现:
```java
public class HashMap<K, V> {
private static final int DEFAULT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private Entry<K, V>[] table;
private int size;
private int threshold;
private float loadFactor;
public HashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity <= 0 || loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new IllegalArgumentException();
}
this.loadFactor = loadFactor;
this.table = new Entry[initialCapacity];
this.threshold = (int) (initialCapacity * loadFactor);
}
public void put(K key, V value) {
if (key == null) {
putForNullKey(value);
return;
}
int hash = hash(key.hashCode());
int index = indexFor(hash, table.length);
for (Entry<K, V> e = table[index]; e != null; e = e.next) {
if (e.hash == hash && (e.key == key || key.equals(e.key))) {
e.value = value;
return;
}
}
addEntry(hash, key, value, index);
}
public V get(K key) {
if (key == null) {
return getForNullKey();
}
int hash = hash(key.hashCode());
int index = indexFor(hash, table.length);
for (Entry<K, V> e = table[index]; e != null; e = e.next) {
if (e.hash == hash && (e.key == key || key.equals(e.key))) {
return e.value;
}
}
return null;
}
private void putForNullKey(V value) {
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
e.value = value;
return;
}
}
addEntry(0, null, value, 0);
}
private V getForNullKey() {
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
return e.value;
}
}
return null;
}
private void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K, V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
if (size++ >= threshold) {
resize(2 * table.length);
}
}
private void resize(int newCapacity) {
Entry<K, V>[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == Integer.MAX_VALUE) {
threshold = Integer.MAX_VALUE;
return;
}
Entry<K, V>[] newTable = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int) (newCapacity * loadFactor);
}
private void transfer(Entry<K, V>[] newTable) {
Entry<K, V>[] src = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) {
Entry<K, V> e = src[j];
if (e != null) {
src[j] = null;
do {
Entry<K, V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
}
private int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
private int indexFor(int hash, int length) {
return hash & (length - 1);
}
static class Entry<K, V> {
final int hash;
final K key;
V value;
Entry<K, V> next;
Entry(int hash, K key, V value, Entry<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
}
```
这个实现使用了数组和链表来存储键值对,当添加新的键值对时,会根据键的哈希值来计算出它在数组中的位置,如果该位置已经有了其他键值对,就使用链表来解决冲突。当数组中的键值对数量超过了阈值时,会自动扩容数组。