LRU 算法 Java 实现

算法 Java 面试 About 3,851 words

定义

Least Recently Used

最近最少使用,是一种常用的页面置换算法。

选择最近最久未使用的数据予以淘汰。

LinkedHashMap

public class LRU_LinkedHashMap {

    public static void main(String[] args) {

        int capacity = 3;

        LinkedHashMap<Object, Object> map = new LinkedHashMap<Object, Object>(capacity, 0.75f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest) {
                return super.size() > capacity;
            }
        };

        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        System.out.println(map.keySet());
        map.put(4, "d");
        System.out.println(map.keySet());
        map.get(2);
        System.out.println(map.keySet());
        map.put(5, "e");
        System.out.println(map.keySet());

    }

}

输出:

[1, 2, 3]
[2, 3, 4]
[3, 4, 2]
[4, 2, 5]

自定义数据结构

主要思路:

  • 定义Map和双向链表
  • put/get时,先从双向链表中删除该元素,再添加到链表头部
public class LRU_Demo {

    public static void main(String[] args) {
        LRU_Demo demo = new LRU_Demo(3);
        demo.put(1, 1);
        demo.put(2, 3);
        demo.put(3, 3);
        System.out.println(demo.map.keySet());

        demo.put(4, 4);
        System.out.println(demo.map.keySet());

        demo.get(2);
        System.out.println(demo.map.keySet());

        demo.put(5, 5);
        System.out.println(demo.map.keySet());
    }

    int capacity;

    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRU_Demo(int capacity) {
        this.capacity = capacity;
        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)) {
            // update
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (map.size() == capacity) {
                // 容量满了,删除最后一个元素
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            //
            Node<Integer, Integer> node = new Node<>(key, value);
            map.put(key, node);
            doubleLinkedList.addHead(node);
        }

    }


    static class Node<K, V> {
        public K key;
        public V value;

        public Node<K, V> prev;
        public Node<K, V> next;

        public Node() {
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    static class DoubleLinkedList<K, V> {
        public Node<K, V> head;
        public Node<K, V> tail;

        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;

            node.next = null;
            node.prev = null;
        }

        public Node<K, V> getLast() {
            return tail.prev;
        }
    }

}

输出:

[1, 2, 3]
[2, 3, 4]
[2, 3, 4]
[2, 4, 5]
Views: 1,590 · Posted: 2022-04-17

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh