LRU Cache

The LRU caching scheme is to remove the least recently used frame when the cache is full and a new page is referenced which is not there in cache.

We use two data structures to implement an LRU Cache.

1. A Queue which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size).
The most recently used pages will be near front end and least recently pages will be near rear end.

2. A HashMap with page number as key and address of the corresponding queue node as value.

Time Complexity:

The LRU cache is a hash table of keys and double linked nodes. The hash table makes the time of get() to be O(1). The list of double linked nodes make the nodes adding/removal operations O(1).

class Node{
    int key;
    int value;
    Node pre;
    Node next;
    public Node(int key, int value){
        this.key = key;
        this.value = value;

public class LRUCache {
    int capacity;
    HashMap<Integer, Node> map = new HashMap<Integer, Node>();
    Node head=null;
    Node end=null;
    public LRUCache(int capacity) {
        this.capacity = capacity;
    public int get(int key) {
        if(map.containsKey(key)) {
            Node n = map.get(key);
            return n.value;
        return -1;
    public void remove(Node n){
            head =;
   = n.pre;
            end = n.pre;
    public void setHead(Node n){ = head;
        n.pre = null;
            head.pre = n;
        head = n;
        if(end ==null)
            end = head;
    public void set(int key, int value) {
            Node old = map.get(key);
            old.value = value;
            Node created = new Node(key, value);
            map.put(key, created);

Lucky for Java already provides a class that is very suitable for our purpose – LinkedHashMap. This class maintains the entries in a HashMap for fast lookup at the same time maintains a doubly linked list of the entries either in AccessOrder or InsertionOrder. This is configurable so use use AccessOrder as true. It also has a method removeOldestEntry() which we can override to return true when the cache size exceeds the specified capacity(upper limit). So here is the implementation.

import java.util.LinkedHashMap;
import java.util.Map.Entry;
public class LRUCache < K, V > extends LinkedHashMap < K, V > {
    private int capacity; // Maximum number of items in the cache.
    public LRUCache(int capacity) { 
        super(capacity+1, 1.0f, true); // Pass 'true' for accessOrder.
        this.capacity = capacity;
    protected boolean removeEldestEntry(Entry entry) {
        return (size() > this.capacity);

Src for the above post :,,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s