算法-手写LRU

方法一:通过LinkedHashMap

package mycode.leetcode;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 苗晓强
 * @date 2023/9/24 13:27
 */
public class MyLRUCache extends LinkedHashMap {

    private int capacity;

    public MyLRUCache(int capacity) {
        /**
         * 注意第三个参数的值
         * true 按照访问的顺序展示集合元素
         * false 按照插入的顺序访问元素
         */
        super(capacity, 0.75F,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {

        MyLRUCache myLRUCache = new MyLRUCache(3);
        myLRUCache.put(1,"a");
        myLRUCache.put(2,"b");
        myLRUCache.put(3,"c");
        System.out.println(myLRUCache.keySet());
        myLRUCache.put(4,"d");
        System.out.println(myLRUCache.keySet());
        myLRUCache.put(3,"c");
        myLRUCache.put(3,"c");
        myLRUCache.put(3,"c");
        System.out.println(myLRUCache.keySet());
        myLRUCache.put(5,"e");
        System.out.println(myLRUCache.keySet());

    }
}

方法二:通过创建节点和双向链表

package mycode.leetcode;

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

/**
 * @author 苗晓强
 * @date 2023/9/24 13:43
 */
public class MyLRUCacheNoJDK {
    //内部类Node 作为数据载体
    class Node{
        K key;
        V value;
        Node pre;
        Node next;

        public Node() {
            this.pre = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.pre = this.next = null;
        }
    }
    //双向队列,存放Node
    class DoubleLinkedList{
        Node head;
        Node tail;
        //构造方法,创建一个头尾节点相连的链表
        public DoubleLinkedList() {
            head = new Node<>();
            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.pre.next = node.next;
            node.next.pre = node.pre;
            node.pre = null;
            node.next = null;
        }
        //获取最后一个节点
        public Node getLast(){
            return tail.pre;
        }
    }

    private int cacheSize;
    Map> map;
    DoubleLinkedList doubleLinkedList;

    public MyLRUCacheNoJDK(int cacheSize){
        this.cacheSize = cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int getKey(int key){
        if (!map.containsKey(key)){
            return -1;
        }
        Node 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 node = map.get(key);
            node.value = value;
            map.put(key,node);

            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (map.size() == cacheSize){
                Node lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            Node newNode = new Node<>(key, value);
            map.put(key,newNode);
            doubleLinkedList.addHead(newNode);

        }
    }

    public static void main(String[] args) {
        MyLRUCacheNoJDK myLRUCache = new MyLRUCacheNoJDK(3);
        myLRUCache.put(1,1);
        myLRUCache.put(2,2);
        myLRUCache.put(3,3);
        System.out.println(myLRUCache.map.keySet());
        myLRUCache.put(4,1);
        System.out.println(myLRUCache.map.keySet());
        myLRUCache.put(3,1);
        myLRUCache.put(3,1);
        myLRUCache.put(3,1);
        System.out.println(myLRUCache.map.keySet());
        myLRUCache.put(5,1);
        System.out.println(myLRUCache.map.keySet());
    }
}

你可能感兴趣的:(算法,java,开发语言)