Skip to content

Commit e13065e

Browse files
committed
💡 Documenting source code.
1 parent 6855d5d commit e13065e

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/main/java/com/crossoverjie/actual/LRUMap.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,39 @@ public class LRUMap<K, V> {
2121
private int nodeCount;
2222

2323

24+
/**
25+
* 头结点
26+
*/
2427
private Node<K, V> header;
28+
29+
/**
30+
* 尾结点
31+
*/
2532
private Node<K, V> tailer;
2633

2734
public LRUMap(int queueSize) {
2835
this.queueSize = queueSize;
36+
//头结点的下一个结点为空
2937
header = new Node<>();
3038
header.next = null;
3139

40+
//尾结点的上一个结点为空
3241
tailer = new Node<>();
3342
tailer.tail = null;
3443

35-
//双向链表
44+
//双向链表 头结点的上结点指向尾结点
3645
header.tail = tailer;
46+
47+
//尾结点的下结点指向头结点
3748
tailer.next = header;
3849

3950

4051
}
4152

4253
public void put(K key, V value) {
4354
cacheMap.put(key, value);
55+
56+
//双向链表中添加结点
4457
addNode(key, value);
4558
}
4659

@@ -50,9 +63,10 @@ private void addNode(K key, V value) {
5063

5164
//容量满了删除最后一个
5265
if (queueSize == nodeCount) {
66+
//删除尾结点
5367
delTail();
5468

55-
//写入表头
69+
//写入头结点
5670
addHead(node);
5771
} else {
5872
addHead(node);
@@ -70,11 +84,13 @@ private void addNode(K key, V value) {
7084
*/
7185
private void addHead(Node<K, V> node) {
7286

87+
//写入头结点
7388
header.next = node;
7489
node.tail = header;
7590
header = node;
7691
nodeCount++;
7792

93+
//如果写入的数据大于2个 就将初始化的头尾结点删除
7894
if (nodeCount == 2) {
7995
tailer.next.next.tail = null;
8096
tailer = tailer.next.next;
@@ -83,6 +99,7 @@ private void addHead(Node<K, V> node) {
8399
}
84100

85101
private void delTail() {
102+
//把尾结点从缓存中删除
86103
cacheMap.remove(tailer.getKey());
87104

88105
//删除尾结点

0 commit comments

Comments
 (0)