zoukankan      html  css  js  c++  java
  • LRUCache

    public class LRUCache<K, V> extends LinkedHashMap<K, V> {
    
        private static final long serialVersionUID = 3125099067311409227L;
    
        private Integer cacheSize;
    
        public LRUCache(Integer cacheSize) {
            super(cacheSize * 3/ 4 + 1, 0.75f, true);
            this.cacheSize = cacheSize;
        }
    
        @Override
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            if (size() > cacheSize) {
                return true;
            }
            return false;
    
            //return super.removeEldestEntry(eldest);
        }
    
    
    }
    public class MyTest {
    
        public static void main(String[] args) {
    
            Map<String, String> map = new LRUCache<>(4);
            map.put("1", "1");
            map.put("2", "2");
            map.put("3", "3");
            map.put("4", "4");
            map.put("5", "5");
            map.put("6", "6");
            map.put("1", "1");
    
    
            //map.get("3");
            //map.get("6");
            //map.get("1");
    
            for (Map.Entry entry : map.entrySet()) {
                System.out.println(entry.getKey() + "=========" + entry.getValue());
            }
    
        }
    }

    运行结果:

    4=========4
    5=========5
    6=========6
    1=========1
  • 相关阅读:
    ORM之F和Q
    ORM查询
    Django
    jQuery基础
    DOM和BOM
    saas baas paas iaas 的理解
    分布式架构的演进过程
    tomcat 配置https 证书
    idea 学习总结
    简单数据库连接池-总结
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/10670764.html
Copyright © 2011-2022 走看看