LinkedHashMap实现Lru

package com.example.demo.test.imp;

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

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

    }

    static class lru extends LinkedHashMap{
        private int lruSize;

        public lru(int lruSize){
            super((int)Math.ceil(lruSize/0.75)+1,0.75f,true);
            this.lruSize = lruSize;
        }

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

 

你可能感兴趣的:(LinkedHashMap实现Lru)