Skip to content

Commit e761870

Browse files
committed
LinkedHashMap
1 parent e45ec2e commit e761870

File tree

2 files changed

+74
-24
lines changed

2 files changed

+74
-24
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.algorithm.study.demo.LRUCache;
2+
3+
import java.util.*;
4+
5+
/**
6+
* LinkedHashMap实现LRU缓存
7+
* @Author: liuxun
8+
* @CreateDate: 2019/2/13 上午10:24
9+
* @Version: 1.0
10+
*/
11+
public class LRUCache {
12+
MapCache cache;
13+
public LRUCache(int capacity) {
14+
this.cache = new MapCache(capacity);
15+
}
16+
17+
public int get(int key) {
18+
return cache.getOrDefault(key, -1);
19+
}
20+
21+
public void put(int key, int value) {
22+
cache.put(key, value);
23+
}
24+
public Collection<Map.Entry<Integer, Integer>> getAll() {
25+
return new ArrayList<Map.Entry<Integer, Integer>>(cache.entrySet());
26+
}
27+
class MapCache extends LinkedHashMap<Integer, Integer> {
28+
public int max;
29+
public MapCache(int max) {
30+
super(max, 0.75f, true);
31+
this.max = max;
32+
}
33+
protected boolean removeEldestEntry(Map.Entry<Integer,Integer> eldest) {
34+
return size() > max;
35+
}
36+
37+
}
38+
public static void main(String[] args) {
39+
LRUCache map = new LRUCache(2) ;
40+
map.put(1,1);
41+
System.out.println(map.get(4));
42+
for (Map.Entry<Integer, Integer> e : map.getAll()){
43+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
44+
}
45+
}
46+
}
47+
48+

src/main/java/com/algorithm/study/demo/LRUCache/LRULinked.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* @CreateDate: 2019/1/23 下午4:03
1010
* @Version: 1.0
1111
*/
12-
public class LRULinked<K,V>{
12+
public class LRULinked{
1313
//缓存
14-
private final Map<K, V> cacheMap = new HashMap<>();
14+
private final Map<Integer, Integer> cacheMap = new HashMap<>();
1515
//根节点
16-
private Node<K, V> root;
16+
private Node root;
1717
private int cacheSize;
1818
private int size;
1919
public LRULinked(int cacheSize){
@@ -24,14 +24,15 @@ public LRULinked(int cacheSize){
2424
* 插入头结点
2525
* @param value
2626
*/
27-
public void put(K key,V value){
28-
Node<K, V> node=new Node<K, V>(key,value);
27+
public void put(int key,int value){
28+
cacheMap.put(key,value);
29+
Node node=new Node(key,value);
2930
if (size==cacheSize){//容量满了删除尾节点
30-
Node<K, V> temp=root.next;
31+
Node temp=root.next;
3132
if (temp==null){
3233
root=null;
3334
}else{
34-
Node<K, V> current=root;
35+
Node current=root;
3536
while (temp.next!=null){
3637
current=temp;
3738
temp=temp.next;
@@ -44,10 +45,13 @@ public void put(K key,V value){
4445
root=node;
4546
size++;
4647
}
47-
public V get(K key){
48-
for (Node<K,V> node = root; node!=null&&!root.key.equals(key); node=node.next){
48+
public int get(int key){
49+
if (!cacheMap.containsKey(key)){
50+
return -1;
51+
}
52+
for (Node node = root; node!=null&&!root.key.equals(key); node=node.next){
4953
if (node.next.key.equals(key)){
50-
Node<K, V> nodeNew=new Node<K, V>(node.next.key,node.next.value);
54+
Node nodeNew=new Node(node.next.key,node.next.value);
5155
node.next=node.next.next;
5256
size--;
5357
this.put(nodeNew.key,nodeNew.value);//查找的节点放到头结点
@@ -60,7 +64,7 @@ public V get(K key){
6064
public String toString(){
6165
StringBuilder sb=new StringBuilder();
6266
int j=0;
63-
Node<K, V> temp=root;
67+
Node temp=root;
6468
while (j<size){
6569
sb.append(temp.value);
6670
temp= temp.next;//找到最后一个结点
@@ -69,26 +73,24 @@ public String toString(){
6973
return sb.toString();
7074
}
7175

72-
public static class Node<K, V>{
73-
private K key;
74-
private V value;
75-
private Node<K, V> next;
76-
public Node(K key, V value){
76+
public static class Node{
77+
private Integer key;
78+
private Integer value;
79+
private Node next;
80+
public Node(Integer key, Integer value){
7781
this.key=key;
7882
this.value=value;
7983
}
8084
}
8185

8286
public static void main(String[] args) {
83-
LRULinked<String,String> linked=new LRULinked<>(3);
84-
linked.put("a","a");
85-
linked.put("b","b");
86-
linked.put("c","c");
87-
linked.get("b");
88-
linked.put("d","d");
87+
LRULinked linked=new LRULinked(3);
88+
linked.put(1,2);
89+
linked.put(2,2);
90+
linked.put(3,3);
91+
System.out.println(linked.get(1));
92+
linked.put(4,4);
8993
System.out.println(linked.size);
9094
System.out.println(linked.toString());
91-
System.out.println(linked.toString());
92-
System.out.println(linked.size);
9395
}
9496
}

0 commit comments

Comments
 (0)