LinkedHashMap 作为一个 CacheMap

Posted Evan Hu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于LinkedHashMap 作为一个 CacheMap的主要内容,如果未能解决你的问题,请参考以下文章

序列化为 GWT AutoBean 时如何保留 LinkedHashMap 的顺序?

LinkedHashMap简明

linkedhashmap如何输出最后几条记录

彻头彻尾理解 LinkedHashMap

JDK LinkedHashMap

LinkedHashMap源码