hashMap中remove()中Node<K,V> node = null, e;是什么意思
时间: 2023-05-20 22:02:28 浏览: 78
这段代码是在HashMap的remove()方法中,用于删除指定键值对的节点。其中,Node<K,V> node = null是定义一个空的节点,e则是用于遍历链表的节点。在遍历链表时,如果找到了要删除的节点,就将该节点赋值给node,然后将该节点从链表中删除。
相关问题
hashMap中remove()的源码解析
HashMap中remove()的源码解析如下:
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
在这段代码中,首先会根据传入的key计算出它的hash值,然后调用removeNode()方法来删除对应的节点。如果删除成功,就返回被删除节点的value值,否则返回null。
removeNode()方法的源码如下:
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) {
Node<K,V>[] tab;
Node<K,V> p;
int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e;
K k;
V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
在这段代码中,首先会根据hash值找到对应的节点p,然后遍历p的链表,找到对应的节点node。如果找到了node,并且它的value值与传入的value值相等,就删除这个节点,并返回它。如果没有找到对应的节点,就返回null。
手写hashmap
好的,这里是一个简单的手写hashmap的实现:
```java
public class MyHashMap<K, V> {
private int size;
private int capacity;
private Node<K, V>[] table;
private static class Node<K, V> {
final K key;
V value;
Node<K, V> next;
Node(K key, V value, Node<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
public MyHashMap(int capacity) {
this.size = 0;
this.capacity = capacity;
this.table = new Node[capacity];
}
public V get(K key) {
int index = hash(key);
Node<K, V> node = table[index];
while (node != null) {
if (node.key.equals(key)) {
return node.value;
}
node = node.next;
}
return null;
}
public void put(K key, V value) {
int index = hash(key);
Node<K, V> node = table[index];
while (node != null) {
if (node.key.equals(key)) {
node.value = value;
return;
}
node = node.next;
}
// If the key does not exist in the map, add a new node to the front of the list at the index.
Node<K, V> newNode = new Node<>(key, value, table[index]);
table[index] = newNode;
size++;
// If the map has exceeded its load factor, resize the table.
if (size > capacity * 0.75) {
resize();
}
}
public void remove(K key) {
int index = hash(key);
Node<K, V> node = table[index];
Node<K, V> prev = null;
while (node != null) {
if (node.key.equals(key)) {
if (prev == null) {
table[index] = node.next;
} else {
prev.next = node.next;
}
size--;
return;
}
prev = node;
node = node.next;
}
}
private int hash(K key) {
return key.hashCode() % capacity;
}
private void resize() {
capacity *= 2;
Node<K, V>[] newTable = new Node[capacity];
for (Node<K, V> node : table) {
while (node != null) {
Node<K, V> next = node.next;
int index = hash(node.key);
node.next = newTable[index];
newTable[index] = node;
node = next;
}
}
table = newTable;
}
}
```
这个HashMap使用链表来解决冲突,并且在size超过capacity*0.75时会自动扩容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)