LRU IntroductionLRU is the abbreviation of Least Recently Used, which is a commonly used page replacement algorithm that selects the pages that have not been used for the longest time for elimination. Simply put, for a set of data, for example: int[] a = {1,2,3,4,5,6}, if the numbers 1 and 2 are often used, they will be ranked after 3,4,5,6, and the array becomes as follows: int[] a = {3,4,5,6,1,2}. If a number is not often used, it will be ranked first! The LRU algorithm is generally used for querying hot data, such as news information. The more news is viewed by users, the more likely it is to be viewed by other users. For news that is rarely accessed, it is basically stored in the ocean! In Java, there is such a collection class that implements this function, it is LinkedHashMap! Introduction to LinkedHashMapWe all know that in Java collections, LinkedHashMap inherits from HashMap, and the underlying data structure is a doubly linked list. Unlike HashMap, LinkedHashMap has a parameter accessOrder in the initialization phase, which defaults to false. public class LinkedHashMap<K,V> If true is passed in, the most recently accessed elements will be placed at the end of the linked list in the order of access. The test is as follows: public static void main(String[] args) { The output is as follows: acessOrderFalse:{1=1, 2=2, 3=3, 4=4} It can be seen that when we set accessOrder to true, frequently accessed elements will be placed in the front! We use this feature to implement an LRU cache using LinkedHashMap as follows:
Among them, removeEldestEntry() means that if it returns true, the element that has not been used recently will be removed. If it returns false, no operation will be performed. This method will be called every time add() is performed. Create an LRU cache class with the following content: public class LRULinkedHashMap<K, V> extends LinkedHashMap<K, V> { Test use: public static void main(String[] args) { The output is as follows: Initial cache content: {1=a, 2=b, 3=c} Initial cache content: {1=a, 2=b, 3=c} After querying the element with key 2, cache content: {1=a, 3=c, 2=b} After adding a new element, cache content: {3=c, 2=b, 4=d} |
<<: What are the deployments and arrangements for 5G in 2022? MIIT responds
iWebFusion (or iWFHosting) is a subsidiary of the...
SpartanHost has launched the 2023 Black Friday pr...
[[433169]] The Wi-Fi Alliance announced on Tuesda...
On May 10, Huawei's Industry Perception Distr...
According to GlobalData, Europe is leading the w...
LOCVPS is a long-established Chinese hosting comp...
Today, the use and growth of mobile technology ha...
Recently, the "IEEE 802 Nendica Report: The ...
When we watch spy movies, we often see undergroun...
In response to the livelihood issue of "spee...
Recently, Shandong issued six standards in the fi...
System administrators use Syslog or SNMP Trap for...
We have shared edgeNAT several times in the tribe...
Overview The original intention of the project wa...
Hello everyone, I am Bernie, an IT pre-sales engi...