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
In an enterprise operation and maintenance enviro...
The "Zhuoshi Network Security Cup National F...
Today, demand for faster Internet and data transm...
RackNerd has officially added a self-service IPv4...
spinservers has sent the latest 2023 US Labor Day...
On February 28, the MWC2023 China Telecom-Huawei ...
I recently received an email from GreenCloudVPS, ...
"End-to-end" is popular nowadays. Let...
On February 8, according to data released by GSA,...
This article will introduce the basic concepts, a...
PacificRack released several special products thi...
Tribe has shared information about OpLink twice i...
[[397802]] This article is reprinted from the WeC...
1. SPI driver source file directory Linux common ...
The operator called to inform me that the package...