LRU least recently used 与LinkedHashMap

Java83

LRU ,最近最少使用淘汰算法,用于存储 限量limit的数据,不超过 limit的数据将直接存储,若超过limit,则将"最老的数据" 淘汰掉。使用LinkedHashMap实现。

LinkedHashMap相较于HashMap增加了 排序,但又比TreeMap less cost

LinkedHashMap继承自HashMap,在entry间维系一个双向链表,但具有预指定迭代顺序,通常为插入顺序。这种情况下,如果key被re-inserted,其顺序不会改变,即更新key并不会改变原有顺序。

但还可以指定排序为按entry最后访问排序。

A special {@link #LinkedHashMap(int,float,boolean) constructor} is provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order).  This kind of map is well-suited to building LRU caches.

其基础api与HashMap时间cost类似,Performance is likely to be just slightly below that of

但LinkedHashMap迭代器耗时与map的size相关,而非capacity。

```

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长