![]() |
In Java, an LRU (Least Recently Used) cache is a data structure that stores a limited number of items and removes the least recently used item when the limit is reached. It is commonly used in computer systems to improve performance by keeping frequently used data readily available, In this article, we will learn the implementation of LRU cache using TreeMap in Java. LRU Cache Implementation using TreeMapBelow is the algorithm of LRU Cache Implementation: Algorithm:
get() method: If the key is present in pair,
If the key is not present, return -1. put() method: If the key is already present in pair,
If the key is not present,
Java Program of LRU Cache Implementation using TreeMapJava
Output
Going to test the TreeMap LRU Cache Implementation Value for the key: 1 is 10 Value for the key: 2 is -1 Value for the key: 1 is -1 Value for the key: 3 is 30 Value for the key: 4 is 40 Explanation of the Program:
Time Complexity:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |