Skip to content

Commit

Permalink
💡 Documenting source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed Apr 4, 2018
1 parent e13065e commit e4e010e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/main/java/com/crossoverjie/actual/LRUMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,69 @@ public void put(K key, V value) {
addNode(key, value);
}

public V get(K key){

Node<K, V> node = getNode(key);

//移动到头结点
moveToHead(node) ;

return cacheMap.get(key);
}

private void moveToHead(Node<K,V> node){

//如果是最后的一个节点
if (node.tail == null){
node.next.tail = null ;
tailer = node.next ;
nodeCount -- ;
}

//如果是本来就是头节点 不作处理
if (node.next == null){
return ;
}

//如果处于中间节点
if (node.tail != null && node.next != null){
//它的上一节点指向它的下一节点 也就删除当前节点
node.tail.next = node.next ;
nodeCount -- ;
}

//最后在头部增加当前节点
//注意这里需要重新 new 一个对象,不然原本的node 还有着下面的引用,会造成内存溢出。
node = new Node<>(node.getKey(),node.getValue()) ;
addHead(node) ;

}

/**
* 链表查询 效率较低
* @param key
* @return
*/
private Node<K,V> getNode(K key){
Node<K,V> node = tailer ;
while (node != null){

if (node.getKey().equals(key)){
return node ;
}

node = node.next ;
}

return null ;
}


/**
* 写入头结点
* @param key
* @param value
*/
private void addNode(K key, V value) {

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

@Override
public String toString() {
return cacheMap.toString();
StringBuilder sb = new StringBuilder() ;
Node<K,V> node = tailer ;
while (node != null){
sb.append(node.getKey()).append(":")
.append(node.getValue())
.append("-->") ;

node = node.next ;
}


return sb.toString();
}
}
48 changes: 48 additions & 0 deletions src/test/java/com/crossoverjie/actual/LRUMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,52 @@ public void put() throws Exception {
System.out.println(lruMap.toString());
}

@Test
public void get() throws Exception {
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
lruMap.put("1",1) ;
lruMap.put("2",2) ;
lruMap.put("3",3) ;

System.out.println(lruMap.toString());
System.out.println("==============");

Integer integer = lruMap.get("1");
System.out.println(integer);
System.out.println("==============");
System.out.println(lruMap.toString());
}

@Test
public void get3() throws Exception {
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
lruMap.put("1",1) ;
lruMap.put("2",2) ;
lruMap.put("3",3) ;

System.out.println(lruMap.toString());
System.out.println("==============");

Integer integer = lruMap.get("2");
System.out.println(integer);
System.out.println("==============");
System.out.println(lruMap.toString());
}

@Test
public void get4() throws Exception {
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
lruMap.put("1",1) ;
lruMap.put("2",2) ;
lruMap.put("3",3) ;

System.out.println(lruMap.toString());
System.out.println("==============");

Integer integer = lruMap.get("3");
System.out.println(integer);
System.out.println("==============");
System.out.println(lruMap.toString());
}

}

0 comments on commit e4e010e

Please sign in to comment.