How to implement a LRU algorithm?

LRU is Least recently used, least recently used, if space is insufficient eliminate the least recently used data.
There are ways to achieve it in three ways:

1, singly linked list

How to implement a LRU algorithm?

The principle:
1, Insert: insert data is the latest data, directly into the tail of the list
2, look for: Find data is moved to the tail of the list, it represents the latest visit.
3. Delete: head of the list represents the least recently used, if the list is full, the head of the list data is deleted

Time complexity: O (n)

2, a doubly linked list hash table +

How to implement a LRU algorithm?

1, the insertion has five nodes, each node in the list 2.
2, black list, the list after representatives hash collision.
3, the order of the list between the red, the representative node.
4, each node consists of four parts, namely pre pointer data, the next pointer list black, red and next pointer list.

Find: Search through the hash table, and then move to the end of a two-way linked list.
Delete: hash tables to look through, get through the precursor node doubly linked list, just delete it.
Insert: look at whether there is, if there is movement to the tail of the list, if not, then delete the full list head node, and then insert the tail of the list, otherwise dissatisfied, then directly into the tail of the list.

3、LinkedHashMap

LinkedHashMap inherited from HashMap, HashMap has all the features, but LinkedHashMap increased head and tail pointers for implementing a doubly linked list.

How to implement a LRU algorithm?

1, LinkedHashMap default support for saving data in accordance with the insertion order, the new data into the doubly linked list tail.
How to implement a LRU algorithm?
How to implement a LRU algorithm?
2, constructed in accordance with the access method supports the order, the latest data access into a doubly linked list tail.
How to implement a LRU algorithm?
How to implement a LRU algorithm?

4, how to achieve it LRU algorithm to automatically delete the expired data list head?

LinkedHashMap default is not automatically delete the list head node data, we need a way to cover the class: removeEldestEntry

1, to achieve a class inherits LinkedHashMap LruLinkedMap

package com.jane;

import java.util.LinkedHashMap;

public class LruLinkedMap<K,V> extends LinkedHashMap<K,V> {

    private int size;

    public LruLinkedMap(int initialCapacity,
               float loadFactor,
               boolean accessOrder) {
        super(initialCapacity, loadFactor, accessOrder);
        this.size = initialCapacity;
    }

    /**
     * @description 重写LinkedHashMap中的removeEldestEntry方法,当LRU中元素多余6个时,
     *              删除最不经常使用的元素
     */
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
        if(size() > size){
            return true;
        }
        return false;
    }

}

2, the startup file:

package com.jane;

import java.util.Map;

public class Main {

    public static void main(String[] args) {
        LruLinkedMap<String, String> ss = new LruLinkedMap(5, 0.75f, true);

        ss.put("1", "3");
        ss.put("2", "4");
        ss.put("3", "6");
        ss.put("4", "1");
        ss.put("5", "5");

        for(Map.Entry e: ss.entrySet()) {
            System.out.println(e.getKey());
        }
        System.out.println("---------------");

        ss.get("1");

        for(Map.Entry e: ss.entrySet()) {
            System.out.println(e.getKey());
        }
        System.out.println("---------------");
        ss.put("7", "10");

        for(Map.Entry e: ss.entrySet()) {
            System.out.println(e.getKey());
        }
    }
}

3. Results:
How to implement a LRU algorithm?

In this way it is easy to achieve the LRU.

Guess you like

Origin blog.51cto.com/janephp/2435311