引言
本文就 《基于LinkedHashMap实现LRU缓存调度算法原理及应用 》一文作为材料,记录一些常见问题,备忘。
延伸出两道常见的Java面试题:
- 插入Entry节点到table表的链表中时,Hashmap 和LinkedHashmap使用头茶法还是尾茶法?遍历map的时候,Entry.Entryset()获取的set集合,是按照从头到尾还是从尾到头的顺序存储的?
- 实现LRU算法最合适的数据结构?
如果读者可以打出来,不用继续看下边的资料了。初学者请继续阅读。相信你读完之后可以找到问题的答案。
LinkedHashMap基础
LinkedHashMap继承了HashMap底层是通过Hash表+单向链表实现Hash算法,内部自己维护了一套元素访问顺序的列表。
Java代码
void init() {
header = new Entry<K,V>(-1, null, null, null);
header.before = header.after = header;
}
Java代码
/**
* 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;
Java代码
/**
* Constructs an empty <tt>LinkedHashMap</tt> instance with the
* specified initial capacity, load factor and ordering mode.
*
* @param initialCapacity the initial capacity.
* @param loadFactor the load factor.
* @param accessOrder the ordering mode - <tt>true</tt> for
* access-order, <tt>false</tt> for insertion-order.
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive.
*/
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
LRU算法
使用有访问顺序排序方式实现LRU,那么哪里LinkedHashMap是如何实现LRU的呢?
Java代码
//LinkedHashMap方法
public V get(Object key) {
Entry<K,V> e = (Entry<K,V>)getEntry(key);
if (e == null)
return null;
e.recordAccess(this);
return e.value;
}
//HashMap方法
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
Java代码
/**
* This method is invoked by the superclass whenever the value
* of a pre-existing entry is read by Map.get or modified by Map.set.
* If the enclosing Map is access-ordered, it moves the entry
* to the end of the list; otherwise, it does nothing.
*/
void recordAccess(HashMap<K,V> m) {
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
if (lm.accessOrder) {
lm.modCount++;
remove();
addBefore(lm.header);
}
}
/**
* Remove this entry from the linked list.
*/
private void remove() {
before.after = after;
after.before = before;
}
/**
* Insert this entry before the specified existing entry in the list.
*/
private void addBefore(Entry<K,V> existingEntry) {
after = existingEntry;
before = existingEntry.before;
before.after = this;
after.before = this;
}
Java代码
/**
* This override alters behavior of superclass put method. It causes newly
* allocated entry to get inserted at the end of the linked list and
* removes the eldest entry if appropriate.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
createEntry(hash, key, value, bucketIndex);
// Remove eldest entry if instructed, else grow capacity if appropriate
Entry<K,V> eldest = header.after;
if (removeEldestEntry(eldest)) {
removeEntryForKey(eldest.key);
} else {
if (size >= threshold)
resize(2 * table.length);
}
}
/**
* This override differs from addEntry in that it doesn't resize the
* table or remove the eldest entry.
*/
void createEntry(int hash, K key, V value, int bucketIndex) {
HashMap.Entry<K,V> old = table[bucketIndex];
Entry<K,V> e = new Entry<K,V>(hash, key, value, old);
table[bucketIndex] = e;
e.addBefore(header);
size++;
}
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}
import java.util.LinkedHashMap; public class URLLinkedListHashMap<K, V> extends LinkedHashMap<K, V> { /** * */ private static final long serialVersionUID = 1L; /** 最大数据存储容量 */ private static final int LRU_MAX_CAPACITY = 1024; /** 存储数据容量 */ private int capacity; /** * 默认构造方法 */ public URLLinkedListHashMap() { super(); } /** * 带参数构造方法 * @param initialCapacity 容量 * @param loadFactor 装载因子 * @param isLRU 是否使用lru算法,true:使用(按方案顺序排序);false:不使用(按存储顺序排序) */ public URLLinkedListHashMap(int initialCapacity, float loadFactor, boolean isLRU) { super(initialCapacity, loadFactor, isLRU); capacity = LRU_MAX_CAPACITY; } public URLLinkedListHashMap(int initialCapacity, float loadFactor, boolean isLRU,int lruCapacity) { super(initialCapacity, loadFactor, isLRU); this.capacity = lruCapacity; } @Override protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) { // TODO Auto-generated method stub return super.removeEldestEntry(eldest); } }
测试代码:
import java.util.LinkedHashMap; import java.util.Map.Entry; public class LRUTest { public static void main(String[] args) { LinkedHashMap<String, String> map = new URLLinkedListHashMap(16, 0.75f, false); map.put("a", "a"); //a a map.put("b", "b"); //a a b map.put("c", "c"); //a a b c map.put("a", "a"); // b c a map.put("d", "d"); //b b c a d map.put("a", "a"); // b c d a map.put("b", "b"); // c d a b map.put("f", "f"); //c c d a b f map.put("g", "g"); //c c d a b f g map.get("d"); //c a b f g d for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.get("a"); //c b f g d a for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.get("c"); //b f g d a c for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.get("b"); //f g d a c b for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.put("h", "h"); //f f g d a c b h for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); } }
答案:
- 插入Entry节点到table表的链表中时,Hashmap 和LinkedHashmap使用头茶法。遍历map的时候,Entry.Entryset()获取的set集合,是按照从尾到头的顺序存储的,采用FIFO原理打印。
- 实现LRU算法最合适的数据结构是LinkedHashmap