10 Line Java Code implementation recently used (LRU) cache

Source: Internet
Author: User

In a recent interview, I was asked several times how to implement a least recently used (LRU) cache. Caching can be done through a hash table, but increasing the size limit for this cache can become another interesting issue. Now let's see how it's going to come true.

Least recently Used cache recycling

To achieve cache recycling, we need to do it easily:

    • Querying for the most recently used item
    • Make a mark on a recently used item

The linked list enables both operations. Detecting the least Recently used item only needs to return the tail of the linked list. Marking an item for recent use only needs to be removed from the current location, and then put the item to the head. The more difficult thing is how to quickly find the item in the list.

Help with a hash table

Looking at the data structure in our toolbox, a hash table can be indexed to an object within the time (consumption) constant. If we create a hash table that is shaped like a key-> linked list node, we can find the most recently used node in constant time. What's more, we can also judge whether the node exists (or does not exist) in constant time.

Once we have found this node, we can move the node to the front of the list and mark it as the most recently used item.

A shortcut to Java

As far as I know, there are very few standard libraries in a programming language that have a common data structure that can provide the functionality described above. This is a mixed data structure, and we need to build a list on the basis of a hash table. But Java has provided us with this form of data structure-linkedhashmap! It even provides a way to overwrite the recycling strategy (see Removeeldestentry documentation). The only thing that needs our attention is that the order in which the lists are changed is the order of the insertions, not the order of the accesses. However, there is a constructor that provides an option to use the order of access (see document).

No need to say more:

Import Java.util.linkedhashmap;import java.util.map;public lrucache<k, v> extends Linkedhashmap<k, V> {  private int cacheSize;  Public LRUCache (int cacheSize) {    super (+, 0.75, true);    This.cachesize = cacheSize;  }  Protected Boolean removeeldestentry (Map.entry<k, v> eldest) {    return size () >= cacheSize;}  }

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

10 Line Java Code implementation recently used (LRU) cache

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.