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
[[386495]] This article is reprinted from the WeC...
Guizhou Tianhetan Scenic Area is famous for its t...
British telecom operator Virgin Media O2 (VMO2) h...
In 2016-2017, the trend of IoT was widely accepte...
Some people say that the most profound change tha...
The Federal Aviation Administration (FAA) said it...
According to industry media reports, some well-kn...
The tribe has shared information about DiyVM many...
The Industrial Internet is the key cornerstone an...
[[179053]] Forcepoint , a global cybersecurity le...
Preface I've been reading about HTTP recently...
As software-defined wide area networks (SD-WAN) b...
CloudCone launched the SSD VPS Flash Sale yesterd...
I think everyone is still curious about this ques...
The protection concept of "invisible, no sec...