@@ -7,7 +7,7 @@ public class LRUCache<K, V> {
7
7
/**
8
8
* 默认容量
9
9
*/
10
- private int DEFAULT_CAPACITY = 1024 ;
10
+ private static final int DEFAULT_CAPACITY = 1024 ;
11
11
/**
12
12
* 缓存容量
13
13
*/
@@ -19,19 +19,21 @@ public class LRUCache<K, V> {
19
19
/**
20
20
* 高效访问的散列表
21
21
*/
22
- private Map<K , Node<V > > map;
22
+ private Map<K , Node<K , V > > map;
23
23
24
- private Node<V > head, tail;
24
+ private Node<K , V > head, tail;
25
25
26
26
/**
27
27
* 自定义双向链表中的节点
28
28
*/
29
- private static class Node <V> {
29
+ private static class Node <K, V> {
30
+ K key;
30
31
V value;
31
- Node<V > prev;
32
- Node<V > next;
32
+ Node<K , V > prev;
33
+ Node<K , V > next;
33
34
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;
35
37
this . value = value;
36
38
this . prev = prev;
37
39
this . next = next;
@@ -46,14 +48,18 @@ public class LRUCache<K, V> {
46
48
}
47
49
48
50
public LRUCache (int capacity ) {
51
+ if (capacity <= 0 ) {
52
+ throw new IllegalArgumentException (" Capacity must be positive integer" );
53
+ }
54
+
49
55
this . capacity = capacity;
50
56
this . map = new HashMap<> (capacity, 0.75F );
51
57
this . head = null ;
52
58
this . tail = null ;
53
59
}
54
60
55
61
public V get (K key ) {
56
- Node<V > node = this . map. get(key);
62
+ Node<K , V > node = this . map. get(key);
57
63
if (node != null ) {
58
64
this . moveToHead(node);
59
65
return node. value;
@@ -63,19 +69,19 @@ public class LRUCache<K, V> {
63
69
}
64
70
65
71
public V put (K key , V value ) {
66
- Node<V > node = this . map. get(key);
72
+ Node<K , V > node = this . map. get(key);
67
73
if (node != null ) {
68
74
node. value = value;
69
75
moveToHead(node);
70
76
return value;
71
77
}
72
78
73
79
if (size == capacity) {
74
- removeLast();
75
- map. remove(key);
80
+ node = removeLast();
81
+ map. remove(node . key);
76
82
}
77
83
78
- node = addFirst(value);
84
+ node = addFirst(key, value);
79
85
map. put(key, node);
80
86
81
87
return value;
@@ -84,9 +90,9 @@ public class LRUCache<K, V> {
84
90
/**
85
91
* 对于新添加的元素,应将新元素添加到链表的头部
86
92
*/
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);
90
96
head = newNode;
91
97
if (h == null ) {
92
98
tail = newNode;
@@ -101,9 +107,9 @@ public class LRUCache<K, V> {
101
107
/**
102
108
* 对于被访问的元素,将该元素移动到头部
103
109
*/
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;
107
113
108
114
if (prev == null ) { // 如果是首节点,无需移动
109
115
return node;
@@ -127,15 +133,14 @@ public class LRUCache<K, V> {
127
133
/**
128
134
* 缓存满时,应删除(淘汰)最后一个节点
129
135
*/
130
- private V removeLast () {
131
- final Node<V > t = tail;
136
+ private Node< K , V > removeLast () {
137
+ final Node<K , V > t = tail;
132
138
if (t == null ) {
133
139
return null ;
134
140
}
135
141
136
- V element = t. value;
137
142
t. value = null ; // help GC
138
- Node<V > prev = t. prev;
143
+ Node<K , V > prev = t. prev;
139
144
t. prev = null ; // help GC
140
145
tail = prev; // 移动 tail
141
146
if (prev == null ) { // 如果尾节点的前一个节点也为空,说明尾节点也是首节点
@@ -144,7 +149,7 @@ public class LRUCache<K, V> {
144
149
prev. next = null ;
145
150
}
146
151
size-- ;
147
- return element ;
152
+ return t ;
148
153
}
149
154
}
150
155
```
0 commit comments