Linkedhashmap is an extension of hashmap, which, depending on the order in which the elements are inserted or the order in which they are accessed (specified by the Accessorderd attribute), uses a doubly linked list to concatenate all elements so that the traversal of the hashmap becomes orderly.
As follows:
(Image referenced from:71713781)
Why design This class?
This implementation is designed to solve hashmap and Hashtable disorder without increasing the high cost of tree operations like TreeMap.
LRU is a cache elimination algorithm, the LRU (Least recently used, farthest use) algorithm based on the historical access records of data to retire data, the core idea is "if the data has been recently accessed, then the chances of being accessed in the future is higher". If the LRU is unclear to the classmate can refer to this article: 79829601
Here is a section of the Linkedhashmap source Notes document:
The above source annotation means that a special constructor Linkedhashmap (Int,float,boolean) of Linkedhashmap is used to create a linkedhashmap that is ordered from the farthest to the most recently accessed order. Such a map is ideal for implementing the LRU cache.
1. Specify Accessorder as true through the constructor
The above-mentioned constructor source code is as follows:
The point is that it sets the Accessorder property, which is defined as follows:
2. Move the element to the end of the list after it has been accessed
When an element is accessed, if Accessorder is true, that is, the map is sorted by Access order, 0 Basic IELTS The element that is accessed is moved to the end of the list, taking the Get method as an example:
As can be seen, in the case of the specified key exists, will determine the Accessorder property, if true, will call the Afternodeaccess (e) method, the source of the method is as follows:
This is in the source code Official document also has the detailed description:
The above document can be divided into several points:
- Calling put, Putifabsent, get, Getordefault, COMPUTE, computeifabsent, computeifpresent, and merge methods will cause access to the corresponding entry.
- The Replace method counts the access action only if value is replaced
- The Putall method generates a single access to each map in the incoming map (according to the iterator order of the map).
In addition, there is no way to generate access to entry. In particular, it is important to note that! Operations in the collection view do not affect the sequence of iterations behind the map.
The above rules can be found from the source of the answer, with the replace () method as an example (the method in HashMap):
As we can see from the above, Afternodeaccess (e) is called only if key exists and the value is replaced. Methods to produce sequential adjustments.
So, in the final analysis, only the afternodeaccess (e) is called in the source code, the node order is adjusted, which is counted as the access operation to the entry.
Linkedhashmap has a preference for LRU strategies, and it specifically designed a method for it:
The description of the method of the document for more than 20 lines, in order to save space, this is not posted here, I will explain to each of them.
First, the function of this method is to decide whether or not to remove eldest elements when new elements are inserted. New Fairway TOEFL It is called when the new element is inserted into the map and is called by the put and Putall methods. The document says, "It provides an opportunity for the implementation to delete the oldest entries each time a new entry is added." This is useful when using map as a cache: it allows map to reduce memory consumption by removing the oldest elements. "Here is a simple example of official document cited:
The effect of this example is that when the map grows to 100, the oldest element is deleted each time the new element is inserted, so that the capacity remains at 100.
Instead of modifying the map directly, this method determines whether the map is allowed to modify itself by the return value. It is also permissible to modify the map directly in this method, but if you do, you must return FALSE to prevent the map from being repeatedly modified.
In the default implementation, this method simply returns false, so the map behaves like a normal map, and the oldest element is never removed. If you need to implement a specific function, we need to override the method as follows:
Often heard that map can be used to implement the cache, when reading this class after the intuitive feeling. From the source of the method can be seen, this class is indeed for the slow stock of custom-made.
Revelation of Linkedhashmap and lru--from source code