Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:29:14+00:00 2026-06-09T20:29:14+00:00

I am a little confused about how to build a LRU cache by using

  • 0

I am a little confused about how to build a LRU cache by using LinkedHashMap (How would you implement an LRU cache in Java 6?), and I want to make sure I understand how it work internally behind the scene.

Lets say I define a class LRUMap that extends the LinkedHashMap and override the removeEldestEntry(final Map.Entry<A, B> eldest) just like the way it is.

Then I construct the data structure and insert 4 items into the map

    LRUMap<String,String> map = new LRUMap<String,String>(3); //capacity 3
    map.put("a", "a");
    map.put("b", "b");
    map.put("c", "c");
    map.put("d", "d");

and interally LinkedHashMap uses an Entry object called header as a starting node to link with all the items that you add to the map. So in this case it will be

   [header] ->  ["a"] -> ["b"] -> ["c"] -> ["d"] -> [header]

The header Entry object is both the start and the end of the doubly linked list since header.before = header.after = header when it initially constructs.

And lets say the map reaches the maximum Entries (3 items) that I want it to be, and from

    Entry<K,V> eldest = header.after;
    if (removeEldestEntry(eldest)) {
         removeEntryForKey(eldest.key);
    }
    .....

So does that mean it will remove [“a”] first ?
And when we call get(Object key) does it rearrange the list order where it puts that key (lets say “b”) before the header node, so it becomes

     [header] ->  ["c"] -> ["d"] -> ["b"] -> [header]

Just want to clarify that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:29:15+00:00Added an answer on June 9, 2026 at 8:29 pm
    1. Yes; the entry <"a", "a"> will be removed first 🙂
    2. Maybe; LinkedHashMap by default uses insertion-order, not access-order…
      straight from the specification:

      Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

      That being said, LinkedHashMap also supports access-order;

      A special constructor is provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). This kind of map is well-suited to building LRU caches. Invoking the put or get method results in an access to the corresponding entry (assuming it exists after the invocation completes).

      So, if you’re using insertion-order, then the order will not change from get("b"); if you’re using access-order (which generally an LRU cache would 😉 then the order will change.

    Any other questions? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little confused about iterators in Java. How do I make next() only
I am a little confused about Accept-Encoding . I have Web Service which would
I am a little confused about the roles of a java application server and
I'm a little confused about render, not quite sure which path it chooses to
I'm a little confused about how to do something that I thought would be
I'm trying to make a simple website using mvc but I'm a little confused
I am a little confused about how I would restructure my database, currently I
I'm a little confused about something. I am using Visual Studio 2010 and am
I am a little confused about open graph I just want my pages to
I am a little confused about when do I have to serialize objects using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.