zoukankan      html  css  js  c++  java
  • LinkedHashMap 作为一个 CacheMap

    今天我想实现一个定数的map,搜索Java Map的实现类,发现了下面的说明:

    url: https://docs.oracle.com/javase/tutorial/collections/implementations/map.html

    LinkedHashMap provides two capabilities that are not available with LinkedHashSet. When you create a LinkedHashMap, you can order it based on key access rather than insertion. In other words, merely looking up the value associated with a key brings that key to the end of the map. Also, LinkedHashMap provides the removeEldestEntry method, which may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map. This makes it very easy to implement a custom cache.

    For example, this override will allow the map to grow up to as many as 100 entries and then it will delete the eldest entry each time a new entry is added, maintaining a steady state of 100 entries.

    private static final int MAX_ENTRIES = 100;
    
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > MAX_ENTRIES;
    }
  • 相关阅读:
    CSRF
    PHP XXE漏洞
    jumpserver安装与部署
    ELK
    docker pull下来的镜像放哪儿了?
    MobSF 框架安装使用部署
    加密流量分析
    Pόlya定理-学习笔记
    所有区间异或的和的 一个加强
    计算一个序列有多少个不同的01子序列
  • 原文地址:https://www.cnblogs.com/evanxyhu/p/LinkedHashMap_as_cacheMap.html
Copyright © 2011-2022 走看看