zoukankan      html  css  js  c++  java
  • linkedhashmap中关于LRU算法的实现

    //LinkedHashMap的一个构造函数,当参数accessOrder为true时,即会按照访问顺序排序,最近访问的放在最前,最早访问的放在后面
    public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {
            super(initialCapacity, loadFactor);
            this.accessOrder = accessOrder;
    }

    LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,LRU缓存就是使用这种原理实现,简单的说就是缓存一定量的数据,当超过设定的阈值时就把一些过期的数据删除掉,比如我们缓存10000条数据,当数据小于10000时可以随意添加,当超过10000时就需要把新的数据添加进来,同时要把过期数据删除,以确保我们最大缓存10000条,那怎么确定删除哪条过期数据呢,采用LRU算法实现的话就是将最老的数据删掉,Java版的LRU缓存实现就数linkedHashmap了

        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>(new CountingMapData(9));
            print(linkedHashMap);
            print("=====");
            linkedHashMap = new LinkedHashMap<>(16, 0.75f, true);
            linkedHashMap.putAll(new CountingMapData(9));
            print(linkedHashMap);
            
            System.out.println("=====");
            for (int i = 0; i < 6; i++) {
                linkedHashMap.get(i);
            }
            print("--"+linkedHashMap);
            linkedHashMap.get(0);
            print("--"+linkedHashMap);
    public class CountingMapData
    extends AbstractMap<Integer,String> {
      private int size;
      private static String[] chars =
        "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
        .split(" ");
      public CountingMapData(int size) {
        if(size < 0) this.size = 0;
        this.size = size;
      }
      private static class Entry
      implements Map.Entry<Integer,String> {
        int index;
        Entry(int index) { this.index = index; }
        public boolean equals(Object o) {
          return Integer.valueOf(index).equals(o);
        }
        public Integer getKey() { return index; }
        public String getValue() {
          return
            chars[index % chars.length] +
            Integer.toString(index / chars.length);
        }
        public String setValue(String value) {
          throw new UnsupportedOperationException();
        }
        public int hashCode() {
          return Integer.valueOf(index).hashCode();
        }
      }
      public Set<Map.Entry<Integer,String>> entrySet() {
        // LinkedHashSet retains initialization order:
        Set<Map.Entry<Integer,String>> entries =
          new LinkedHashSet<Map.Entry<Integer,String>>();
        for(int i = 0; i < size; i++)
          entries.add(new Entry(i));
        return entries;
      }
      public static void main(String[] args) {
        System.out.println(new CountingMapData(60));
      }
    }

    输出:

    {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0}
    =====
    {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0}
    =====
    --{6=G0, 7=H0, 8=I0, 0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0}
    --{6=G0, 7=H0, 8=I0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 0=A0}
  • 相关阅读:
    SCRAM
    package-info.java https://www.intertech.com/Blog/whats-package-info-java-for/
    mybatis-3 cache 源码赏析
    Cache replacement policies 缓存实现算法
    MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参
    Taking a peek inside with the Actuator
    表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
    Why is long2ip conversion important?
    遇到的1/3,在十进位制中是一个无限小数,但在这种进位制中就是一个有限小数。
    并发编程:Actors 模型和 CSP 模型
  • 原文地址:https://www.cnblogs.com/zhangfengshi/p/9947888.html
Copyright © 2011-2022 走看看