Skip to content

Commit 6855d5d

Browse files
committed
✨ Introducing new features.put map ok
1 parent 1491ba2 commit 6855d5d

File tree

2 files changed

+96
-17
lines changed

2 files changed

+96
-17
lines changed

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

+69-17
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,95 @@ public class LRUMap<K, V> {
1818

1919
private int queueSize;
2020

21-
private int nodeCount ;
21+
private int nodeCount;
2222

2323

24-
private Node<K,V> header ;
25-
private Node<K,V> tailer ;
24+
private Node<K, V> header;
25+
private Node<K, V> tailer;
2626

27+
public LRUMap(int queueSize) {
28+
this.queueSize = queueSize;
29+
header = new Node<>();
30+
header.next = null;
31+
32+
tailer = new Node<>();
33+
tailer.tail = null;
34+
35+
//双向链表
36+
header.tail = tailer;
37+
tailer.next = header;
38+
39+
40+
}
2741

2842
public void put(K key, V value) {
2943
cacheMap.put(key, value);
44+
addNode(key, value);
45+
}
3046

47+
private void addNode(K key, V value) {
3148

32-
}
49+
Node<K, V> node = new Node<>(key, value);
3350

34-
private void addNode(K key,V value){
3551
//容量满了删除最后一个
36-
if (queueSize == nodeCount){
37-
tailer.next.tail = null ;
38-
tailer.next = null ;
39-
nodeCount -- ;
52+
if (queueSize == nodeCount) {
53+
delTail();
4054

4155
//写入表头
42-
Node<K,V> node = new Node<>(key,value) ;
43-
header.next = node ;
44-
}else {
56+
addHead(node);
57+
} else {
58+
addHead(node);
4559

4660
}
4761

4862

4963
}
5064

5165

52-
private class Node<K,V> {
53-
private K key ;
54-
private V value ;
55-
Node<K,V> tail ;
56-
Node<K,V> next ;
66+
/**
67+
* 添加头结点
68+
*
69+
* @param node
70+
*/
71+
private void addHead(Node<K, V> node) {
72+
73+
header.next = node;
74+
node.tail = header;
75+
header = node;
76+
nodeCount++;
77+
78+
if (nodeCount == 2) {
79+
tailer.next.next.tail = null;
80+
tailer = tailer.next.next;
81+
}
82+
83+
}
84+
85+
private void delTail() {
86+
cacheMap.remove(tailer.getKey());
87+
88+
//删除尾结点
89+
tailer.next.tail = null;
90+
tailer = tailer.next;
91+
92+
nodeCount--;
93+
94+
}
95+
96+
private class Node<K, V> {
97+
private K key;
98+
private V value;
99+
Node<K, V> tail;
100+
Node<K, V> next;
57101

58102
public Node(K key, V value) {
59103
this.key = key;
60104
this.value = value;
61105
}
62106

107+
public Node() {
108+
}
109+
63110
public K getKey() {
64111
return key;
65112
}
@@ -77,4 +124,9 @@ public void setValue(V value) {
77124
}
78125

79126
}
127+
128+
@Override
129+
public String toString() {
130+
return cacheMap.toString();
131+
}
80132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.crossoverjie.actual;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.sun.org.apache.bcel.internal.generic.LUSHR;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class LRUMapTest {
10+
11+
@Test
12+
public void put() throws Exception {
13+
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
14+
lruMap.put("1",1) ;
15+
lruMap.put("2",2) ;
16+
lruMap.put("3",3) ;
17+
18+
System.out.println(lruMap.toString());
19+
20+
lruMap.put("4",4) ;
21+
System.out.println(lruMap.toString());
22+
23+
lruMap.put("5",5) ;
24+
System.out.println(lruMap.toString());
25+
}
26+
27+
}

0 commit comments

Comments
 (0)