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
Background of MSTP RSTP is an improvement on STP,...
Since April 2021, my country's 5G development...
HTTP is an excellent communication protocol, but ...
[51CTO.com original article] Are you tired of wor...
Preface Hello everyone, I am Amazing. It is the g...
IoT drives SD-WAN adoption The Internet of Things...
At present, various regions continue to accelerat...
[[326829]] This is the last lecture of the "...
On August 9, according to foreign media reports, ...
BudgetVM is still offering a 50% discount on the ...
The tribe has shared a lot of cheap RackNerd VPS ...
For most of the front-end developers interviewed,...
ShockHosting is a foreign hosting company founded...
[51CTO.com original article] A few years ago, whe...
[[392156]] The launch of the digital RMB will def...