码迷,mamicode.com
首页 > 系统相关 > 详细

LinkedHashMap 作为一个 CacheMap

时间:2017-11-20 17:53:00      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:set   rds   ring   ini   cti   turn   doc   delete   nbsp   

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

标签:set   rds   ring   ini   cti   turn   doc   delete   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!