Java实现LRU

首先看看什么是LRU

LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间 t,当须淘汰一个页面时,选择现有页面中其 t 值最大的,即最近最少使用的页面予以淘汰。(来自[百度百科](https://baike.baidu.com/item/LRU/1269842?fr=aladdin))

Java实现

先来捋一下思路,我们需要准备一个双向链表,这样到时候删除cache中很久没有被使用的key的时间复杂度就为n(1)。但是链表的查询时间复杂度为O(n),对此我们引入HashMap进行存储链表节点,HashMap的查询时间复杂度为O(1)。

代码实现如下

import java.util.HashMap;
import java.util.Map;

class LRUCacheTest{
    private class LinkedNode{
        private int key;
        private int val;
        LinkedNode pre;
        LinkedNode next;
        public LinkedNode(){}
        public LinkedNode(int key, int val){
            this.key = key;
            this.val = val;
            this.pre = null;
            this.next = null;
        }
    }
    private Map<Integer, LinkedNode> map = new HashMap<>();
    private int capacity;
    private LinkedNode head;
    private LinkedNode tail;
    public LRUCacheTest(int capacity){
        this.capacity = capacity;
        head = new LinkedNode(-1,-1);
        tail = new LinkedNode(-1,-1);
        head.next = tail;
        head.pre = null;
        tail.pre = tail;
        tail.next = null;
    }
    public int get(int key){
        // 如果该关键字还在cache中,返回其对应的值,如果没有就返回-1
        if(map.containsKey(key)){
            LinkedNode node = map.get(key);
            removeNode(node);
            addToHead(node);
            System.out.print("The value of "+key+" is ");
            return map.get(key).val;
        }
        System.out.print("404, the key you get is not in cache ");
        return -1;
    }

    private void removeNode(LinkedNode node) {
        LinkedNode from = node.pre;
        LinkedNode to = node.next;
        from.next = to;
        to.pre = from;
    }

    private void addToHead(LinkedNode node) {
        node.next = head.next;
        node.pre = head;
        head.next.pre = node;
        head.next = node;
    }
    public void put(int key, int val){
        if(map.containsKey(key)){
            map.remove(key);
            removeNode(map.get(key));
            LinkedNode node = new LinkedNode(key, val);
            map.put(key, node);
            addToHead(node);
            System.out.println(key+" has been put in cache, the value of "+key+" is "+ val);
        }else{
            if(map.size()==capacity){
                int removeKey = removeTailNode();
                map.remove(removeKey);
                LinkedNode node = new LinkedNode(key, val);
                addToHead(node);
                map.put(key,node);
                System.out.println(key+" has been put in cache, the value of "+key+" is "+ val);
            }else{
                LinkedNode node = new LinkedNode(key, val);
                map.put(key,node);
                addToHead(node);
                System.out.println(key+" has been put in cache, the value of "+key+" is "+ val);
            }
        }
    }

    private int removeTailNode() {
        LinkedNode node = tail.pre;
        removeNode(tail.pre);
        return node.key;
    }

}

public class LRUTest01 {
    public static void main(String[] args) {
        LRUCacheTest lru = new LRUCacheTest(3);
        lru.put(1,1);
        lru.put(2,2);
        lru.put(3,3);
        System.out.println(lru.get(1));
        lru.put(4,4);
        System.out.println(lru.get(2));
    }
}

运行结果如下

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值