public class LruCached {
//node节点装载数据
class Node<K, V> {
K key;
V value;
Node pre;
Node next;
public Node() {
this.pre = this.next = null;
}
public Node(K k, V v) {
this.key = k;
this.value = v;
this.next = this.pre = null;
}
}
//双向链表
class DoubleLinkedList<K, V> {
Node<K, V> head;
Node<K, V> tail;
public DoubleLinkedList() {
this.head = new Node<>();
this.tail = new Node<>();
head.next = tail;
tail.pre = head;
}
public void addHead(Node node) {
node.next = head.next;
node.pre = head;
head.next.pre = node;
head.next = node;
}
public void removeNode(Node node) {
node.next.pre = node.pre;
node.pre.next = node.next;
node.pre = null;
node.next = null;
}
public Node getLast() {
return tail.pre;
}
}
private int cacheSize;
Map<Integer, Node<Integer, Integer>> map;
DoubleLinkedList<Integer, Integer> doubleLinkedList;
public LruCached(int cacheSize) {
this.cacheSize = cacheSize;
map = new HashMap<>();
doubleLinkedList = new DoubleLinkedList<>();
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
}
Node<Integer, Integer> node = map.get(key);
doubleLinkedList.removeNode(node);
doubleLinkedList.addHead(node);
return node.value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
Node<Integer, Integer> node = map.get(key);
node.value = value;
map.put(key, node);
doubleLinkedList.removeNode(node);
doubleLinkedList.addHead(node);
} else {
if (map.size() == cacheSize) {
Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
map.remove(lastNode.key);
doubleLinkedList.removeNode(lastNode);
}
Node<Integer, Integer> newNode = new Node<>(key, value);
map.put(key, newNode);
doubleLinkedList.addHead(newNode);
}
}
public static void main(String[] args) {
LruCached lruCached = new LruCached(3);
lruCached.put(1, 1);
lruCached.put(2, 2);
lruCached.put(3, 3);
System.out.println(lruCached.map.keySet());
lruCached.put(4, 4);
System.out.println(lruCached.map.keySet());
lruCached.put(5, 5);
System.out.println(lruCached.map.keySet());
lruCached.put(1, 1);
System.out.println(lruCached.map.keySet());
}
}
手写LRU(出自阳哥教学)