运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得关键字 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得关键字 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,要如何实现呢?
首先看到了LinkedHashMap 底层是一个Entery结点,为了实现插入快有序和快速访问两个优点,结合了HashMap与双向链表
这边不深入去讲,主要来说说LinkedHashMap存储数据是有序的,而且分为两种:插入顺序和访问顺序。下图主要为访问顺序时结点变更情况
这时候,有些小伙伴就会惊讶的发现,这不就是LRU本U吗? 但其实LinkedHashMap是会不断扩容的,要想真正实现LRU,你得去重写removeEldestEntry()方法
重写removeEldestEntry方法,当达到条件,就返回ture ,返回true就删除最近最少使用的Entery结点
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest){
//重写removeEldestEntry方法,当达到条件,就返回ture ,返回true就删除最近最少使用的Entery结点
return super.size()>capacity;
}
/**
* 继承LinkedHashMap实现,将构造方法实现成按访问顺序(true),(false)为按插入顺序即为FIFO
*/
class LRUCache extends LinkedHashMap<Integer,Integer> {
int capacity;
public LRUCache(int capacity) {
super(capacity,075f,true);
this.capacity=capacity;
}
public int get(int key) {
return super.getOrDefault(key,-1); //这里找不到就返回-1
}
public void put(int key, int value) {
super.put(key,value);
}
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest){
//重写removeEldestEntry方法,当达到条件,就返回ture ,返回true就删除最近最少使用的Entery结点
return super.size()>capacity;
}
}
继承LinkedHashMap实现,将构造方法实现成按访问顺序(true),(false)为按插入顺序即为FIFO
既然原理都懂了,不如我们自己手写一个LRU加深印象,不就是一个HashMap跟双向链表吗,干他就完了
双向链表与hashmap结合
对于get操作,判断是否插已插入
对于put操作,判断key是否存在
class DoubleList{
//双向链表
int key;
int value;
DoubleList pre;
DoubleList next;
public DoubleList(int key, int value) {
this.key = key;
this.value = value;
}
public DoubleList() {
}
}
注释基本都有,哪里出错了评论区见
class LRUCache1 {
int size; //当前容量
int capacity; //最大容量
DoubleList head; //头指针
DoubleList tail; //尾指针
Map<Integer,DoubleList> map=new HashMap<>(); //hash快速查找
public LRUCache1(int capacity) {
size=0;
this.capacity=capacity;
head=new DoubleList();
tail=new DoubleList();
head.next=tail;
tail.pre=head;
}
/**
* 查找该结点
* @param key
* @return
*/
public int get(int key) {
DoubleList findVal = map.get(key);
if(findVal!=null){
moveToHead(findVal);
return findVal.value;
}else return -1;
}
/**
* 新增结点
* @param key
* @param value
*/
public void put(int key, int value) {
DoubleList findVal = map.get(key); //查询是否存在该key
if(findVal==null){
//不存在
DoubleList doubleList = new DoubleList(key, value); //新建这个结点
map.put(key,doubleList); //扔到map中去
addToHead(doubleList); //放到链表头部
size++; //自增容量
if(size>capacity) {
//若当前容量大于最大容量
DoubleList removeTail = removeTail(); //找到最后一个结点,删除
map.remove(removeTail.key); //在map中删除该key
size--;
}
}else {
//若该结点存在
findVal.value=value; //更新这个结点的值
moveToHead(findVal); //删除并移动到链表头部
}
}
/**
* 包含两个操作
* 1.在链表中删除该结点
* 2.将该结点加到链表头部
* @param doubleList
*/
private void moveToHead(DoubleList doubleList){
removeNode(doubleList);
addToHead(doubleList);
}
/**
* 在链表中删除该结点
* @param doubleList
*/
private void removeNode(DoubleList doubleList){
doubleList.pre.next=doubleList.next;
doubleList.next.pre=doubleList.pre;
}
/**
* 将该结点头插到链表中
* @param node
*/
private void addToHead(DoubleList node){
// node.pre = head;
// node.next = head.next;
// head.next.pre = node;
// head.next = node;
node.next=head.next;
node.pre=head;
head.next.pre=node;
head.next=node;
}
/**
* 移除队尾元素
* @return 返回被删除的结点(为了删除map中的值,所以此处返回结点)
*/
private DoubleList removeTail() {
DoubleList temp=tail.pre;
removeNode(temp);
return temp;
}
}