How to Implement a Cache Eviction Policy using TreeMap in Java?6 May 2025 | 8 min read Least Recently Used, or LRU is a cache eviction technique that will remove the least recently accessed items from the cache if the cache size has grown to its maximum allotted capacity. Furthermore, the cache must have a robust synchronization mechanism because several threads may access the items stored inside an application. Caching is a technique that uses memory to hold frequently accessed data in order to improve application speed. A cache eviction policy determines which items need to be deleted when the cache fills up. Java's TreeMap provides an implementation of a sorted map that can be used to build a cache with a unique eviction technique. By maintaining the keys' order, a TreeMap enables effective iteration and retrieval. In this case, we will create a cache eviction method that removes the oldest items first using a TreeMap with timestamps as keys. Designing of the LRU:
Java's ConcurrentHashMap (which satisfies properties 1-4) and basic Doubly-Linked-List (which satisfies property 5) can be used to create a primary LRU cache while taking into account the five properties above. To store and retrieve objects using keys, a ConcurrentHashMap is utilized. This allows for concurrent access, provides for the setting of a size restriction, and ensures that access times are in O(1) order. The pointers to the same items (in the map) are maintained by a doubly-linked list (DLL). With the aid of this DLL, we can implement the LRU eviction method, which places the most frequently accessed items toward the end of the list and the least often accessed items near the top. The LRU eviction occurs when a new object is added to the cache and if the cache has filled up to its total capacity. In essence, the head node-the least recently used object-will be eliminated from the DLL and the map will also be cleared of it. Approach: Using TreeMapThe code uses a HashMap for key-value storage (cache), another HashMap for recording access frequencies (freqMap), and a TreeMap (freqCount) to maintain frequency-based ordering in order to create an LFU Cache with a capacity constraint. In order to apply the LFU Cache Eviction policy, we have added key-value pairs and retrieved the values for various keys. Quick retrieval and insertion with organic ordering are guaranteed by TreeMap's mapping of frequencies to sets of keys (LinkedHashSet). On access or modification, the cache dynamically modifies the frequency of keys, and when capacity is reached, it effectively deletes the least utilized key. An object with a capacity of three is created using the LFUCache class, and the values are added and retrieved using the get() method. By taking use of these data structures' characteristics, this approach maximizes lookups and updates. Algorithm:Step 1: Create a fixed-capacity LFU cache by grouping keys according to their frequencies using a TreeMap, storing fundamental values using a HashMap, and tracking frequencies with a HashMap. Step 2: Perform the Get operation Step 2.1: Return null if the key is not in the cache. Step 2.2: If not, update the frequency of the key in both the frequency counter (TreeMap) and the frequency map. Step 2.3: Return the matching value. Step 3: Perform the Put operation Step 3.1: Use the get function to change the key's value and frequency if it exists. Step 3.2: Take out the least utilized key (lowest frequency in the TreeMap) if the cache is full. Step 3.3: Update the frequency map and counter, insert the new key-value combination, and set its frequency to 1. Implementation:FileName: LFUCache.java Output: The Get 1: One The Get 2: null The Get 3: Three The Get 4: Four The Get 5: Five Complexity Analysis: The Time Complexity is O(log F) for each operation, with F representing the number of unique frequencies (usually low in comparison to C), and the space complexity is O(C) where C is the cache capacity. Approach: Using TreeMap and Doubly-Linked ListThe method makes use of TreeMap for frequency-based ordering, HashMap for O(1) key lookups, and Doubly Linked Lists for practical node addition and deletion. While DoublyLinkedList handles nodes of the same frequency, the Node class keeps track of cache entry data like key, value, and frequency. These data structures work together to efficiently update frequency counts and evict the least frequently used (LFU) node when the capacity is exceeded, ensuring maximum performance for LFU cache operations. The technical focus is on employing these structures to maintain memory organization and time complexity efficiency. Algorithm: Step 1: Declare capacity, freqMap (frequency-node list mapping), and nodeMap (key-node mapping). Step 2: Apply Put Operation Step 2.1: If a key is present in the node map, update its frequency and value. Step 2.2: The least frequently used (LFU) node should be removed if the cache size is more than its capacity. Step 2.3: In nodeMap and freqMap, add the new node with frequency 1. Step 3: Apply Get Operation Step 3.1: Return null if the key is not in the nodeMap. Step 3.2: If not, get the node, return its value, and update its frequency. Step 4: The node's current frequency list should be removed. Step 5: Add it to the new frequency list in freqMap and increase its frequency. Step 6: Use freqMap to find the lowest frequency. Step 7: Delete the node in the associated list that hasn't been used recently. Step 8: If nodeMap becomes empty, remove the node and update freqMap. Step 9: Use put and get to illustrate cache operations and demonstrate LFU capability. Implementation:FileName: LFUCacheExample.java Output: The Get 1: One The Get 2: null The Get 3: Three The Get 4: Four The Get 5: Five Complexity Analysis: The Time Complexity is O(log F) for each operation, with F representing the number of unique frequencies (usually low in comparison to C), and the space complexity is O(C) where C is the cache capacity. Next TopicPassing Array to Function In Java |
? Adding two dates is a common task in programming, especially when dealing with time-based calculations. In Java, there are several ways to add two dates together, depending on the specific requirements of the program. In this article, we will explore some of the different methods for...
6 min read
In this section, we will discuss what is local minima in an array and how to find local minima through a Java program. What is local minima in an array? An element is said to be local minima of an array if the array element is less than...
3 min read
Real-time face recognition is the process of identifying or verifying an individual's identity in a real-time video stream. This technology has a wide range of applications, from security and surveillance systems to personalized marketing and entertainment. In this article, we will explore how to implement real-time...
6 min read
Java is a programming language that supports the development of generic classes and methods. Java's generics feature enables programmers to design code that can operate on several object types without the use of typecasting. A generic type must occasionally be changed into a specific type, though....
4 min read
Java is the most popular object-oriented programming language but it has some drawbacks. The major drawback is to write lots of boilerplate code. To overcome this drawback, project Lombok comes into existence. It is a tool that spices up our Java application. In this section,...
13 min read
In the domain of computing and algorithm designing, the median of a dataset is one common type of problem that people face. The median which gives the midpoint of a distribution of values indexed in ascending order of values is another measure of central tendency. However, when...
4 min read
Java is one of the most used programming languages for developing dynamic web applications. A web application is computer software that utilizes the web browser and technologies to perform tasks over the internet. A web application is deployed on a web server. Java provides some technologies like...
8 min read
Difference Between this and super in Java In Java, the keywords "super" and "this" are essential for interacting with classes and objects. In addition to referring class members, they assist in managing inheritance. Java also provides the this() and super() constructors, which are used in the context of constructors. this keyword...
8 min read
Determining if a given string is an even-odd palindrome is the task at sight for a given string str. When the characters at even indices make a palindrome, and the characters at odd indices form a palindrome independently, the string is said to be an...
5 min read
What is TDD? Test-driven development, or TDD for short, is a software development process. As the name implies, involves utilizing tests to guide application development, resulting in simple, iterative implementation with good test coverage right from the start. Test-Driven Designing and building tests for each single function of...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India