• 面试题:手写LRU


    /**
     * @author WGR
     * @create 2021/1/12 -- 17:12
     */
    public class LRUCacheDemo2<K,V> extends LinkedHashMap<K,V> {
    
        private int capacity;
    
        public LRUCacheDemo2(int capacity){
            super(capacity,0.75F,true);
            this.capacity = capacity;
        }
    
        @Override
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            return super.size() > capacity;
        }
    
        public static void main(String[] args) {
            LRUCacheDemo2 lruCacheDemo = new LRUCacheDemo2(3);
            lruCacheDemo.put(1,1);
            lruCacheDemo.put(2,2);
            lruCacheDemo.put(3,3);
            System.out.println(lruCacheDemo.keySet());
            lruCacheDemo.put(4,1);
            System.out.println(lruCacheDemo.keySet());
            lruCacheDemo.put(3,1);
            System.out.println(lruCacheDemo.keySet());
            lruCacheDemo.put(3,1);
            System.out.println(lruCacheDemo.keySet());
            lruCacheDemo.put(5,1);
            System.out.println(lruCacheDemo.keySet());
        }
    }
    

    image-20210112171649506

    /**
     * @author WGR
     * @create 2021/1/12 -- 15:54
     */
    public class LRUCacheDemo {
    
        //构造一个Node节点,作为数据载体
        class Node<K,V>{
            K key;
            V value;
            Node<K,V> prev;
            Node<K,V> next;
            public Node(){
                this.prev = this.next = null;
            }
    
            public Node(K key,V value){
                this.key = key;
                this.value = value;
                this.prev = this.next = null;
            }
        }
    
        //构造一个双向队列,里面放Node
        class DoubleLinkedList<K,V>{
            Node<K,V> head;
            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.prev = null;
                node.next = null;
            }
    
            public Node<K,V> getLast(){
                return tail.prev;
            }
        }
        public  int cacheSize;
        Map<Integer,Node<Integer,Integer>> map;
        DoubleLinkedList<Integer,Integer> doubleLinkedList;
    
        public LRUCacheDemo(int cacheSize){
            this.cacheSize = cacheSize;
            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.addHead(node);
    
            return node.value;
    
        }
    
        public void put(int key,int value){
            if(map.containsKey(key)){
                Node<Integer, Integer> node = map.get(key);
                node.value = value;
                map.put(key,node);
                doubleLinkedList.removeNode(node);
                doubleLinkedList.addHead(node);
            }else{
                if(map.size() == cacheSize){
                    Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                    map.remove(lastNode.key);
                    doubleLinkedList.removeNode(lastNode);
                }
                Node<Integer, Integer> newNode = new Node<>(key, value);
                map.put(key,newNode);
                doubleLinkedList.addHead(newNode);
            }
        }
    
        public static void main(String[] args) {
            LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);
            lruCacheDemo.put(1,1);
            lruCacheDemo.put(2,2);
            lruCacheDemo.put(3,3);
            System.out.println(lruCacheDemo.map.keySet());
            lruCacheDemo.put(4,1);
            System.out.println(lruCacheDemo.map.keySet());
            lruCacheDemo.put(3,1);
            System.out.println(lruCacheDemo.map.keySet());
            lruCacheDemo.put(3,1);
            System.out.println(lruCacheDemo.map.keySet());
            lruCacheDemo.put(5,1);
            System.out.println(lruCacheDemo.map.keySet());
        }
    
    
    }
    
    

    image-20210112170654461

  • 相关阅读:
    MySQL主键和外键使用及说明
    SQLAlchemy
    路飞学城购买流程API
    路飞学城知识点
    使用rest_framework写api接口的一些注意事项(axios发送ajax请求)
    微信推送功能
    支付宝支付业务
    路飞学城前端Vue
    Python爬虫,用第三方库解决下载网页中文本的问题
    Python爬虫,抓取淘宝商品评论内容
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14268012.html
Copyright © 2020-2023  润新知