Objective:
When learning the LRU algorithm, see LRUCache Source code implementation is based on Linkedhashmap, today to learn about the benefits of Linkedhashmap and how to implement the LRU caching mechanism.
Requirements background:
LRU This algorithm is the most recent use time away from the current time the most distant data removed, and implementation of LRUCache will be frequently executed insert, query, delete and so on, we will think of using LinkedList, but we also based on Key-value to save data, This time we will think of HashMap, but HashMap can't keep the data insertion order like LinkedList, and if you want to use HashMap, you can use one of its subclasses linkedhashmap.
Linkedhashmap Introduction:
Linkedhashmap is a hash table and link list implementation of the map interface with a predictable iteration order. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant, and the LINKEDHASHMAP implementation differs from HashMap in that it maintains a double-link list that runs on all items. This list of links defines the order of iterations, which can be in the order of insertion or in the order of access. The order of the elements in the linked list can be divided into: linked lists in the order of insertion, and linked lists in the order of access (called Get methods). The default is to sort by insert order, if you specify in order of access, then when the Get method is called, the element of this access is moved to the tail of the linked list, and the linked list can be sorted in order of access. You can override the Removeeldestentry method to return a true value that specifies that the oldest element is removed when the element is inserted. For more information about LINKEDHASHMAP, please see this blog, blog address: http://www.cnblogs.com/children/archive/2012/10/02/2710624.html
Linkedhashmap using:
Because we're here in order to implement the LRU algorithm, the sort mode is set to true access order ordering
int initialcapacity = 10; // Initialize capacity float loadfactor = 0.75f; // load factor, typically 0.75f Boolean true; // sort by false based on the order of insertion true based on the access order of new linkedhashmap<> (initialcapacity, Loadfactor, Accessorder);
Specific look at the effect:
for(inti = 0; I < 10; i++) {map.put (string.valueof (i), i); } //Pre-access order for(iterator<map.entry<string, integer>> it =Map.entryset (). iterator (); It.hasnext ();) {Map.entry<string, integer> next =It.next (); LOG.E (TAG,"Linkedmap--before-->" +Next.getkey ()); } //Analog AccessMap.get ("5"); //post-Access data for(iterator<map.entry<string, integer>> it =Map.entryset (). iterator (); It.hasnext ();) {Map.entry<string, integer> next =It.next (); LOG.E (TAG,"Linkedmap--after-->" +Next.getkey ()); } }
The results of the run found that the visited 5 unknown has changed the description is based on the access sort. We'll see how to remove the expired.
Final intInitialcapacity = 10;//Initialize Capacity floatLoadfactor = 0.75f;//load factor, typically 0.75f BooleanAccessorder =true;//sort by false based on the insertion order true based on the access order//map<string, integer> Map = new Linkedhashmap<> (initialcapacity, Loadfactor, accessorder);map<string, integer> map =NewLinkedhashmap (initialcapacity, Loadfactor, Accessorder) {@Overrideprotected BooleanRemoveeldestentry (Entry eldest) {returnSize () >initialcapacity; } }; for(inti = 0; I < 15; i++) {map.put (string.valueof (i), i); } //Pre-access order for(iterator<map.entry<string, integer>> it =Map.entryset (). iterator (); It.hasnext ();) {Map.entry<string, integer> next =It.next (); LOG.E (TAG,"Linkedmap--before-->" +Next.getkey ()); }
We have a capacity of 10, we insert 15 we found that the first insertion of the five are missing, indicating that the LRU algorithm works.
The Linkedhashmap of Java Learning