Skip to content

Commit e4e010e

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

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

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

+75-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,69 @@ public void put(K key, V value) {
5757
addNode(key, value);
5858
}
5959

60+
public V get(K key){
61+
62+
Node<K, V> node = getNode(key);
63+
64+
//移动到头结点
65+
moveToHead(node) ;
66+
67+
return cacheMap.get(key);
68+
}
69+
70+
private void moveToHead(Node<K,V> node){
71+
72+
//如果是最后的一个节点
73+
if (node.tail == null){
74+
node.next.tail = null ;
75+
tailer = node.next ;
76+
nodeCount -- ;
77+
}
78+
79+
//如果是本来就是头节点 不作处理
80+
if (node.next == null){
81+
return ;
82+
}
83+
84+
//如果处于中间节点
85+
if (node.tail != null && node.next != null){
86+
//它的上一节点指向它的下一节点 也就删除当前节点
87+
node.tail.next = node.next ;
88+
nodeCount -- ;
89+
}
90+
91+
//最后在头部增加当前节点
92+
//注意这里需要重新 new 一个对象,不然原本的node 还有着下面的引用,会造成内存溢出。
93+
node = new Node<>(node.getKey(),node.getValue()) ;
94+
addHead(node) ;
95+
96+
}
97+
98+
/**
99+
* 链表查询 效率较低
100+
* @param key
101+
* @return
102+
*/
103+
private Node<K,V> getNode(K key){
104+
Node<K,V> node = tailer ;
105+
while (node != null){
106+
107+
if (node.getKey().equals(key)){
108+
return node ;
109+
}
110+
111+
node = node.next ;
112+
}
113+
114+
return null ;
115+
}
116+
117+
118+
/**
119+
* 写入头结点
120+
* @param key
121+
* @param value
122+
*/
60123
private void addNode(K key, V value) {
61124

62125
Node<K, V> node = new Node<>(key, value);
@@ -144,6 +207,17 @@ public void setValue(V value) {
144207

145208
@Override
146209
public String toString() {
147-
return cacheMap.toString();
210+
StringBuilder sb = new StringBuilder() ;
211+
Node<K,V> node = tailer ;
212+
while (node != null){
213+
sb.append(node.getKey()).append(":")
214+
.append(node.getValue())
215+
.append("-->") ;
216+
217+
node = node.next ;
218+
}
219+
220+
221+
return sb.toString();
148222
}
149223
}

src/test/java/com/crossoverjie/actual/LRUMapTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,52 @@ public void put() throws Exception {
2424
System.out.println(lruMap.toString());
2525
}
2626

27+
@Test
28+
public void get() throws Exception {
29+
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
30+
lruMap.put("1",1) ;
31+
lruMap.put("2",2) ;
32+
lruMap.put("3",3) ;
33+
34+
System.out.println(lruMap.toString());
35+
System.out.println("==============");
36+
37+
Integer integer = lruMap.get("1");
38+
System.out.println(integer);
39+
System.out.println("==============");
40+
System.out.println(lruMap.toString());
41+
}
42+
43+
@Test
44+
public void get3() throws Exception {
45+
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
46+
lruMap.put("1",1) ;
47+
lruMap.put("2",2) ;
48+
lruMap.put("3",3) ;
49+
50+
System.out.println(lruMap.toString());
51+
System.out.println("==============");
52+
53+
Integer integer = lruMap.get("2");
54+
System.out.println(integer);
55+
System.out.println("==============");
56+
System.out.println(lruMap.toString());
57+
}
58+
59+
@Test
60+
public void get4() throws Exception {
61+
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
62+
lruMap.put("1",1) ;
63+
lruMap.put("2",2) ;
64+
lruMap.put("3",3) ;
65+
66+
System.out.println(lruMap.toString());
67+
System.out.println("==============");
68+
69+
Integer integer = lruMap.get("3");
70+
System.out.println(integer);
71+
System.out.println("==============");
72+
System.out.println(lruMap.toString());
73+
}
74+
2775
}

0 commit comments

Comments
 (0)