用JAVA手写一个HashMap
时间: 2023-07-16 15:14:30 浏览: 136
java手写本地缓存示例
好的,下面是一个简单的手写HashMap实现,仅供参考:
```java
public class MyHashMap<K, V> {
private static final int DEFAULT_CAPACITY = 16; // 默认容量为16
private static final float DEFAULT_LOAD_FACTOR = 0.75f; // 默认负载因子为0.75
private int size; // 元素个数
private int threshold; // 阈值,当size >= threshold时,需要进行扩容
private float loadFactor; // 负载因子
private Node<K, V>[] table; // 存储元素的数组
public MyHashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public MyHashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public MyHashMap(int initialCapacity, float loadFactor) {
if (initialCapacity <= 0) {
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
}
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
}
this.loadFactor = loadFactor;
this.table = new Node[initialCapacity];
this.threshold = (int) (initialCapacity * loadFactor);
}
public V put(K key, V value) {
if (key == null) {
return putForNullKey(value);
}
int hash = hash(key.hashCode());
int index = indexFor(hash, table.length);
for (Node<K, V> node = table[index]; node != null; node = node.next) {
if (node.hash == hash && (key == node.key || key.equals(node.key))) {
V oldValue = node.value;
node.value = value;
return oldValue;
}
}
addEntry(hash, key, value, index);
return null;
}
private V putForNullKey(V value) {
for (Node<K, V> node = table[0]; node != null; node = node.next) {
if (node.key == null) {
V oldValue = node.value;
node.value = value;
return oldValue;
}
}
addEntry(0, null, value, 0);
return null;
}
private void addEntry(int hash, K key, V value, int index) {
Node<K, V> node = table[index];
table[index] = new Node(hash, key, value, node);
if (++size >= threshold) {
resize();
}
}
private void resize() {
int oldCapacity = table.length;
int newCapacity = oldCapacity * 2;
Node<K, V>[] newTable = new Node[newCapacity];
for (Node<K, V> node : table) {
while (node != null) {
int index = indexFor(node.hash, newCapacity);
Node<K, V> next = node.next;
node.next = newTable[index];
newTable[index] = node;
node = next;
}
}
table = newTable;
threshold = (int) (newCapacity * loadFactor);
}
public V get(K key) {
if (key == null) {
return getForNullKey();
}
int hash = hash(key.hashCode());
int index = indexFor(hash, table.length);
for (Node<K, V> node = table[index]; node != null; node = node.next) {
if (node.hash == hash && (key == node.key || key.equals(node.key))) {
return node.value;
}
}
return null;
}
private V getForNullKey() {
for (Node<K, V> node = table[0]; node != null; node = node.next) {
if (node.key == null) {
return node.value;
}
}
return null;
}
public boolean containsKey(K key) {
return get(key) != null;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
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);
}
private static class Node<K, V> {
private final int hash;
private final K key;
private V value;
private Node<K, V> next;
public Node(int hash, K key, V value, Node<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
}
```
以上是一个简单的HashMap实现,其中采用了链表法解决哈希冲突,并且在元素个数达到阈值时进行了扩容。需要注意的是,这个实现并不完整,仅作为参考。在实际使用中,还需要考虑并发性、迭代器等因素。
阅读全文