JDK中LinkedHashMap中的源码可以看到LRU算法的影子
1、`accessOrder`把它设置为`true`,那么map内部会按照访问顺序进行维护(记得是维护,如何维护的呢?)
在构造LinkedHashMap的时候可以选择一个参数`accessOrder`,默认为`false`,map内部会按照插入顺序进行维护。如果手动把它设置为`true`,那么map内部会按照访问顺序进行维护。
1.1 accessOrder和构造函数的源码如下:
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
{
/**
* The iteration ordering method for this linked hash map: <tt>true</tt>
* for access-order, <tt>false</tt> for insertion-order.
*
* @serial
*/
private final boolean accessOrder;
/**
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
* with the specified initial capacity and load factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public LinkedHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
accessOrder = false;
}
1.2 如何删除老的数据(是如何维护的一部分吧),需要重写方法,源码如下:
/**
* Returns <tt>true</tt> if this map should remove its eldest entry.
* This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
* inserting a new entry into the map. It provides the implementor
* with the opportunity to remove the eldest entry each time a new one
* is added. This is useful if the map represents a cache: it allows
* the map to reduce memory consumption by deleting stale entries.
*
* <p>Sample use: this override will allow the map to grow up to 100
* entries and then delete the eldest entry each time a new entry is
* added, maintaining a steady state of 100 entries.
* <pre>
* private static final int MAX_ENTRIES = 100;
*
* protected boolean removeEldestEntry(Map.Entry eldest) {
* return size() > MAX_ENTRIES;
* }
* </pre>
*
* <p>This method typically does not modify the map in any way,
* instead allowing the map to modify itself as directed by its
* return value. It <i>is</i> permitted for this method to modify
* the map directly, but if it does so, it <i>must</i> return
* <tt>false</tt> (indicating that the map should not attempt any
* further modification). The effects of returning <tt>true</tt>
* after modifying the map from within this method are unspecified.
*
* <p>This implementation merely returns <tt>false</tt> (so that this
* map acts like a normal map - the eldest element is never removed).
*
* @param eldest The least recently inserted entry in the map, or if
* this is an access-ordered map, the least recently accessed
* entry. This is the entry that will be removed it this
* method returns <tt>true</tt>. If the map was empty prior
* to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
* in this invocation, this will be the entry that was just
* inserted; in other words, if the map contains a single
* entry, the eldest entry is also the newest.
* @return <tt>true</tt> if the eldest entry should be removed
* from the map; <tt>false</tt> if it should be retained.
*/
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}
上面源码的注释
* <p>Sample use: this override will allow the map to grow up to 100
* entries and then delete the eldest entry each time a new entry is
* added, maintaining a steady state of 100 entries.
* <pre>
* private static final int MAX_ENTRIES = 100;
*
* protected boolean removeEldestEntry(Map.Entry eldest) {
* return size() > MAX_ENTRIES;
* }
* </pre>
2、写一个实现LRU的例子:
(1)容量是3(对,是3,你没有看错),大于3个就删除最不经常访问的数据(也就是相对更老的数据)
public static void main(String[] args) {
// initialCapacity:16;loadFactor:0.75;accessOrder:true按照访问顺序维护数据
Map<String, String> map = new LinkedHashMap<String, String>(16, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
// 当大于3的时候删除
return size() > 3;
}
};
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
System.out.println("顺序插入了key=a/b/c 3条数据后:" + map);
map.get("a");
map.get("b");
System.out.println("访问了key=a和key=b的2条数据之后的数据:" + map);
map.put("d", "4");
System.out.println("重写了removeEldestEntry,且插入key=4这条数据后(删除了最进没有访问的数据):" + map);
}
结果输出:
顺序插入了key=a/b/c 3条数据后:{a=1, b=2, c=3}
访问了key=a和key=b的2条数据之后的数据:{c=3, a=1, b=2}
重写了removeEldestEntry,且插入key=4这条数据后(删除了最进没有访问的数据):{a=1, b=2, d=4}