Skip to content

Commit 73de2d0

Browse files
committed
♻️ Refactoring code.
1 parent e4e010e commit 73de2d0

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.crossoverjie.actual;
22

3-
import com.sun.scenario.effect.impl.prism.PrImage;
4-
53
import java.util.HashMap;
64
import java.util.Map;
7-
import java.util.concurrent.LinkedBlockingQueue;
85

96
/**
107
* Function:
@@ -16,8 +13,14 @@
1613
public class LRUMap<K, V> {
1714
private final Map<K, V> cacheMap = new HashMap<>();
1815

19-
private int queueSize;
16+
/**
17+
* 最大缓存大小
18+
*/
19+
private int cacheSize;
2020

21+
/**
22+
* 节点大小
23+
*/
2124
private int nodeCount;
2225

2326

@@ -31,8 +34,8 @@ public class LRUMap<K, V> {
3134
*/
3235
private Node<K, V> tailer;
3336

34-
public LRUMap(int queueSize) {
35-
this.queueSize = queueSize;
37+
public LRUMap(int cacheSize) {
38+
this.cacheSize = cacheSize;
3639
//头结点的下一个结点为空
3740
header = new Node<>();
3841
header.next = null;
@@ -125,7 +128,7 @@ private void addNode(K key, V value) {
125128
Node<K, V> node = new Node<>(key, value);
126129

127130
//容量满了删除最后一个
128-
if (queueSize == nodeCount) {
131+
if (cacheSize == nodeCount) {
129132
//删除尾结点
130133
delTail();
131134

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

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ public void put() throws Exception {
2323
lruMap.put("5",5) ;
2424
System.out.println(lruMap.toString());
2525
}
26+
@Test
27+
public void put2() throws Exception {
28+
LRUMap<String,Integer> lruMap = new LRUMap(4) ;
29+
lruMap.put("1",1) ;
30+
lruMap.put("2",2) ;
31+
lruMap.put("3",3) ;
32+
33+
System.out.println(lruMap.toString());
34+
35+
lruMap.put("4",4) ;
36+
System.out.println(lruMap.toString());
37+
38+
lruMap.put("5",5) ;
39+
System.out.println(lruMap.toString());
40+
}
2641

2742
@Test
2843
public void get() throws Exception {

0 commit comments

Comments
 (0)