Skip to content

Commit e033536

Browse files
committed
修复了map无法删除元素的问题
1 parent 91dab8d commit e033536

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

resource/markdown/algorithm/LRUCache.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class LRUCache<K, V> {
77
/**
88
* 默认容量
99
*/
10-
private int DEFAULT_CAPACITY = 1024;
10+
private static final int DEFAULT_CAPACITY = 1024;
1111
/**
1212
* 缓存容量
1313
*/
@@ -19,19 +19,21 @@ public class LRUCache<K, V> {
1919
/**
2020
* 高效访问的散列表
2121
*/
22-
private Map<K, Node<V>> map;
22+
private Map<K, Node<K, V>> map;
2323

24-
private Node<V> head, tail;
24+
private Node<K, V> head, tail;
2525

2626
/**
2727
* 自定义双向链表中的节点
2828
*/
29-
private static class Node<V> {
29+
private static class Node<K, V> {
30+
K key;
3031
V value;
31-
Node<V> prev;
32-
Node<V> next;
32+
Node<K, V> prev;
33+
Node<K, V> next;
3334

34-
Node(Node<V> prev, V value, Node<V> next) {
35+
Node(Node<K, V> prev, K key, V value, Node<K, V> next) {
36+
this.key = key;
3537
this.value = value;
3638
this.prev = prev;
3739
this.next = next;
@@ -46,14 +48,18 @@ public class LRUCache<K, V> {
4648
}
4749

4850
public LRUCache(int capacity) {
51+
if (capacity <= 0) {
52+
throw new IllegalArgumentException("Capacity must be positive integer");
53+
}
54+
4955
this.capacity = capacity;
5056
this.map = new HashMap<>(capacity, 0.75F);
5157
this.head = null;
5258
this.tail = null;
5359
}
5460

5561
public V get(K key) {
56-
Node<V> node = this.map.get(key);
62+
Node<K, V> node = this.map.get(key);
5763
if (node != null) {
5864
this.moveToHead(node);
5965
return node.value;
@@ -63,19 +69,19 @@ public class LRUCache<K, V> {
6369
}
6470

6571
public V put(K key, V value) {
66-
Node<V> node = this.map.get(key);
72+
Node<K, V> node = this.map.get(key);
6773
if (node != null) {
6874
node.value = value;
6975
moveToHead(node);
7076
return value;
7177
}
7278

7379
if (size == capacity) {
74-
removeLast();
75-
map.remove(key);
80+
node = removeLast();
81+
map.remove(node.key);
7682
}
7783

78-
node = addFirst(value);
84+
node = addFirst(key, value);
7985
map.put(key, node);
8086

8187
return value;
@@ -84,9 +90,9 @@ public class LRUCache<K, V> {
8490
/**
8591
* 对于新添加的元素,应将新元素添加到链表的头部
8692
*/
87-
private Node<V> addFirst(V e) {
88-
final Node<V> h = head;
89-
final Node<V> newNode = new Node<>(null, e, h);
93+
private Node<K, V> addFirst(K key, V value) {
94+
final Node<K, V> h = head;
95+
final Node<K, V> newNode = new Node<>(null, key, value, h);
9096
head = newNode;
9197
if (h == null) {
9298
tail = newNode;
@@ -101,9 +107,9 @@ public class LRUCache<K, V> {
101107
/**
102108
* 对于被访问的元素,将该元素移动到头部
103109
*/
104-
private Node moveToHead(Node<V> node) {
105-
final Node<V> prev = node.prev;
106-
final Node<V> next = node.next;
110+
private Node<K, V> moveToHead(Node<K, V> node) {
111+
final Node<K, V> prev = node.prev;
112+
final Node<K, V> next = node.next;
107113

108114
if (prev == null) { // 如果是首节点,无需移动
109115
return node;
@@ -127,15 +133,14 @@ public class LRUCache<K, V> {
127133
/**
128134
* 缓存满时,应删除(淘汰)最后一个节点
129135
*/
130-
private V removeLast() {
131-
final Node<V> t = tail;
136+
private Node<K, V> removeLast() {
137+
final Node<K, V> t = tail;
132138
if (t == null) {
133139
return null;
134140
}
135141

136-
V element = t.value;
137142
t.value = null; // help GC
138-
Node<V> prev = t.prev;
143+
Node<K, V> prev = t.prev;
139144
t.prev = null; // help GC
140145
tail = prev; // 移动 tail
141146
if (prev == null) { // 如果尾节点的前一个节点也为空,说明尾节点也是首节点
@@ -144,7 +149,7 @@ public class LRUCache<K, V> {
144149
prev.next = null;
145150
}
146151
size--;
147-
return element;
152+
return t;
148153
}
149154
}
150155
```

0 commit comments

Comments
 (0)