首页 > 系统服务 > 详细

LinkedHashMap 作为一个 CacheMap

时间:2017-11-20 17:13:03      阅读:212      评论:0      收藏:0      [点我收藏+]

今天我想实现一个定数的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

原文:http://www.cnblogs.com/evanxyhu/p/LinkedHashMap_as_cacheMap.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!