手写简单版LRU算法
public class LRU<K,V> {
private int capacity;
private HashMap<K,Node<K,V>> map;
private Node head;
private Node tail;
public LRU(int capacity) {
this.capacity = capacity;
map = new HashMap<K,Node<K,V>>();
this.head = this.tail = new Node();
this.head.next = this.tail;
this.tail.pre = this.head;
}
public void put(K key,V value){
Node<K,V> node = new Node<K,V>(key,value);
if(!map.containsKey(key)){
node.pre = this.head;
node.next = this.head.next;
this.head.next = node;
node.next.pre = node;
if(map.size() == capacity){
delNode(getLastNode());
}
map.put(key,node);
}else{
moveToHead(map.get(key));
map.get(key).setValue(value);
}
}
public V get(K key){
if(null != map.get(key)){
moveToHead(map.get(key));
return map.get(key).value;
}
return null;
}
private void moveToHead(Node<K,V> node){
delNode(node);
node.pre = this.head;
node.next = this.head.next;
this.head.next = node;
node.next.pre = node;
map.put(node.key,node);
}
private Node getLastNode(){
return tail.pre;
}
private void delNode(Node<K,V> node){
node.pre.next = node.next;
node.next.pre = node.pre;
map.remove(node.key);
}
@Override
public String toString() {
String result = "";
Node node = tail;
while (node.pre != head){
node = node.pre;
result += node.key.toString();
}
return result;
}
static class Node<K,V>{
private K key;
private V value;
private Node pre;
private Node next;
public Node() {
}
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void main(String[] args) {
LRU<Integer,Integer> lru = new LRU<>(4);
lru.put(1,1);
lru.put(2,2);
lru.put(3,3);
System.out.println(lru.toString());
lru.put(4,4);
System.out.println(lru.toString());
lru.put(5,5);
System.out.println(lru.toString());
System.out.println(lru.get(4));
System.out.println(lru.toString());
System.out.println(lru.get(6));
}
}
基于LinkedHashMap实现的LRU算法
public class LinkHashMapLRU<K,V> extends LinkedHashMap<K,V> {
private int capacity;
public LinkHashMapLRU(int capacity) {
super(capacity,0.75f,true);
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return super.size() > capacity;
}
public static void main(String[] args) {
LinkHashMapLRU<Integer,Integer> linkHashMapLRU = new LinkHashMapLRU<Integer,Integer>(3);
linkHashMapLRU.put(1,1);
linkHashMapLRU.put(2,2);
linkHashMapLRU.put(3,3);
System.out.println(linkHashMapLRU.keySet().toString());
linkHashMapLRU.put(4,4);
System.out.println(linkHashMapLRU.keySet().toString());
linkHashMapLRU.put(3,3);
linkHashMapLRU.put(3,3);
linkHashMapLRU.put(3,3);
System.out.println(linkHashMapLRU.keySet().toString());
linkHashMapLRU.put(5,5);
System.out.println(linkHashMapLRU.keySet().toString());
}
}