LRU cache implementation in java

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.

In this post, we will see LRU cache implementation in java.


Problem

Design a Least recently used cache implementation in java. It should have below properties.

bounded size: It should have bounded size to take care of memory limits.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Fast access: As we are designing cache, we should be able to fetch or update entries faster.

Evict least recently used entry: Cache should evict least recently used entry if capacity is reached.


Solution

Using HashMap and Doubly linked list

As we need to do lookup in O(1) time then HashMap is obvious choice but we need to take care of least recently used entry as well.

We need to find a data structure which can remove/add in O(1) time if we already know the node. We can use a double linked list for this purpose because it provides removal/addition in O(1) time if already know about the node.

HashMap will make get operation in O(1) time and Doube linked list will make removal/addition in O(1) time.

LRU cache

 

Here is simple program for LRU cache implementation in java.

When you run above program, you will get below output:

94
97
-1

As you can see, in last 4 entries, we do not have 15 as key. That’s the reason we are getting -1 for it.

Using LinkedHashMap

We can use LinkedHashMap to create LRU cache as well. LinkedHashMap has a constructor in which you can specify access order. If you pass accessOrder as true, LinkedHashMap will work on the basis of access order rather than insertion order.

We will also override a method named removeEldestEntry() and it will return true in case size() is greater than capacity.

Let’s create a class Named LRUCache

When you run above program, you will get output:

94
97
-1

As you can see, in last 4 entries, we do not have 15 as key. That’s the reason we are getting -1 for it.

That’s all about LRU cache implementation in java.

Related Posts

  • Data structures in java
    16 April

    Data Structures in java

    In this post, we will see about various data structures in java. Data structure is a way of storing and organizing data. Data structure provide a way to process and store data efficiently. For example: 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to […]

  • 29 November

    Top 100+ Java coding interview questions

    I have been posting data structure and coding interview questions on various topics such as Array, Queue, Stack, Binary tree, LinkedList, String, Number, ArrayList, etc. So I am consolidating a list of java coding interview questions to create an index post. I will keep adding links to this post whenever I will add new java […]

  • 18 April

    Minimum Number of Jumps to reach last Index

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to find Minimum Number of Jumps to reach last Index. Problem Given an array A of positive integers possibly zeroes, every index indicating the maximum length of a jump that can […]

  • 20 February

    Inorder Successor in a Binary Search Tree

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to find Inorder Successor in a Binary Search Tree. Problem Given a Binary Search Tree and a target node value. Find the Inorder successor of the given node value in the […]

  • 04 February

    LCA of a K-ary Tree in O(Sqrt(height))

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find Lowest Common Ancestor of a K-ary Tree in O(Sqrt(height)).We have already seen how to find LCA of n-ary tree in O(n) complexity. Problem Given a K-ary tree and […]

  • 03 February

    Largest Rectangular Area in a Histogram

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find largest rectangular area in a Histogram. Problem Given an Integer representing number of bars in a Histogram and an array of integers representing the height of the bars […]

Comments

  1. I am big fan of your blog. It has best interview preparation and concepts learning materials. Please keep adding more design implementations that’s been frequently asked in interviews these days. Thanks for your effort:)

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.