緩存這個東西就是爲了提高運行速度的,由於緩存是在寸土寸金的內存裏面,不是在硬盤裏面,所以容量是很有限的。LRU這個算法就是把最近一次使用時間離現在時間最遠的數據刪除掉。先說說List:每次訪問一個元素後把這個元素放在 List一端,這樣一來最遠使用的元素自然就被放到List的另一端。緩存滿了t的時候就把那最遠使用的元素remove掉。但更實用的是HashMap。因爲List太慢,要刪掉的數據總是位於List底層數組的第一個位置,刪掉之後,後面的數據要向前補位。。所以複雜度是O(n),那就用鏈表結構的LinkedHashMap唄~,LinkedHashMap默認的元素順序是put的順序,但是如果使用帶參數的構造函數,那麼LinkedHashMap會根據訪問順序來調整內部 順序。 LinkedHashMap的get()方法除了返回元素之外還可以把被訪問的元素放到鏈表的底端,這樣一來每次頂端的元素就是remove的元素。
構造函數如下:
public LinkedHashMap (int initialCapacity, float loadFactor, boolean accessOrder);
initialCapacity 初始容量
loadFactor 加載因子,一般是 0.75f
accessOrder false 基於插入順序 true 基於訪問順序(get一個元素後,這個元素被加到最後,使用了LRU 最近最少被使用的調度算法)
- import java.util.*;
- class Test
- {
- public static void main(String[] args) throws Exception{
- Map<Integer,Integer> map=new LinkedHashMap<Integer,Integer>(10,0.75f,true);
- map.put(9,3);
- map.put(7,4);
- map.put(5,9);
- map.put(3,4);
- //現在遍歷的話順序肯定是9,7,5,3
- //下面訪問了一下9,3這個鍵值對,輸出順序就變嘍~
- map.get(9);
- for(Iterator<Map.Entry<Integer,Integer>> it=map.entrySet().iterator();it.hasNext();){
- System.out.println(it.next().getKey());
- }
- }
- }
5
3
9
- import java.util.*;
- //擴展一下LinkedHashMap這個類,讓他實現LRU算法
- class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V>{
- //定義緩存的容量
- private int capacity;
- private static final long serialVersionUID = 1L;
- //帶參數的構造器
- LRULinkedHashMap(int capacity){
- //調用LinkedHashMap的構造器,傳入以下參數
- super(16,0.75f,true);
- //傳入指定的緩存最大容量
- this.capacity=capacity;
- }
- //實現LRU的關鍵方法,如果map裏面的元素個數大於了緩存最大容量,則刪除鏈表的頂端元素
- @Override
- public boolean removeEldestEntry(Map.Entry<K, V> eldest){
- System.out.println(eldest.getKey() + "=" + eldest.getValue());
- return size()>capacity;
- }
- }
- //測試類
- class Test{
- public static void main(String[] args) throws Exception{
- //指定緩存最大容量爲4
- Map<Integer,Integer> map=new LRULinkedHashMap<Integer,Intege>(4);
- map.put(9,3);
- map.put(7,4);
- map.put(5,9);
- map.put(3,4);
- map.put(6,6);
- //總共put了5個元素,超過了指定的緩存最大容量
- //遍歷結果
- for(Iterator<Map.Entry<Integer,Integer>> it=map.entrySet().iterator();it.hasNext();){
- System.out.println(it.next().getKey());
- }
- }
- }
9=3
9=3
9=3
9=3
7
5
3
6