当前位置:   article > 正文

Java 实现 LRU 算法_java lru

java lru

1 什么是LRU

LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰。

2 实现思路

开始时,内存中没有页面。
每次访问页面时,先检测内存中是否存在该页面,若不存在则将该页面加载到内存“末尾”,若存在则直接访问该页面,并将该页面移到内存“末尾”。
如果访问某个内存中不存在的页面时,内存已满,则将内存“开头”的页面移出,并将新的页面加载到内存“末尾”。
这样就可以始终保持着最近访问的页面在不经常访问的页面的后面了。

3数据结构的选择

3.1 数组

  • 页面命中时,需要将其移动到数组末尾。
  • 页面命中失败时,需要删除数组头部元素,并将所有元素向前移动一步,然后在末尾添加元素。

这两个操作都涉及大量元素的移动,时间复杂度为O(n),效率较低。

3.2 链表

虽然不涉及到元素的移动,但是检查页面是否命中需要遍历链表,时间复杂度也为O(n)

3.3 双向链表 + 哈希表

双向链表维护最近使用的和很久没有使用页面的先后顺序,哈希表用于定位页面在双向链表中的位置,方便查找。
在这里插入图片描述
用双向链表是因为删除链表节点时,需要知道该节点的前驱节点。
双向链表两边特意加上虚拟头节点和虚拟伪节点,以使得链表两端元素的插入和删除操作与链表中间的元素保持一致。

有点类似于Java提供的集合LinkedHashMap。借助LinkedHashMap很容易实现LRU算法,我这里就不展示了。不借助LinkedHashMap的代码实现:

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

/**
 * @Desc 采用LRU置换算法的缓存
 * @Author WK
 * @Date 2022/4/3 19:44
 */
public class LRUCache<K, V> {

    // 静态内部类,双向链表中的节点类,key理解为页面号,val理解为页面内容
    static class Entry<K, V> {
        public Entry<K, V> prev;
        public Entry<K, V> next;
        public K key;
        public V val;
        public Entry() {}
        public Entry(K key, V val) { this.key = key; this.val = val; }
    }

    private Entry<K, V> head, tail; // 虚拟头节点和虚拟尾节点
    private final int capacity;     // 缓存容量
    private int size;               // 缓存占用量
    Map<K, Entry<K, V>> cache;      // 哈希表,记录双向列表节点的地址值

    public LRUCache(int capacity) {
        this.capacity = capacity;
        initCache();
    }

    // 初始化LRU缓存
    private void initCache() {
        head = new Entry<>();
        tail = new Entry<>();
        head.next = tail;
        tail.prev = head;
        size = 0;
        cache = new HashMap<>(this.capacity);
    }

    private V get(K key) {
        Entry<K, V> entry = cache.get(key);
        if(entry != null) {
            moveToTail(entry);
            return entry.val;
        } else {
            return null;
        }
    }

    private void put(K key, V val) {
        Entry<K, V> entry = cache.get(key);
        if(entry != null) {
            // 缓存命中
            entry.val = val;
            moveToTail(entry);
        } else {
            // 缓存未命中
            if(size == capacity) {
                // 缓存已满,删除链表头部节点
                Entry<K, V> h = head.next;
                deleteEntry(h);
                cache.remove(h.key);
                size--;
            }
            // 添加新页面到链表尾部
            Entry<K, V> newEntry = new Entry<>(key, val);
            addToTail(newEntry);
            cache.put(key, newEntry);
            size++;
        }
    }

    private void moveToTail(Entry<K, V> entry) {
        deleteEntry(entry);
        addToTail(entry);
    }

    private void addToTail(Entry<K, V> entry) {
        if(entry != null) {
            entry.next = tail;
            entry.prev = tail.prev;
            tail.prev.next = entry;
            tail.prev = entry;
        }
    }

    private void deleteEntry(Entry<K, V> entry) {
        if(entry != null) {
            entry.prev.next = entry.next;
            entry.next.prev = entry.prev;
        }
    }

    public static void main(String[] args) {
        LRUCache<Integer, String> cache = new LRUCache<>(2);
        cache.put(1,"可口可乐");
        cache.put(2,"雪碧");
        System.out.println("页面1的内容:" + cache.get(1));
        cache.put(3,"果粒橙"); // 此时缓存已满,且页面2最久未被使用(因为cache.get(1)访问了页面1),页面2被置换成页面3
        System.out.println("页面2的内容:" + cache.get(2)); // 页面2已被换出,访问不到
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

运行结果:

页面1的内容:可口可乐
页面2的内容:null
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/942342
推荐阅读
相关标签
  

闽ICP备14008679号