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 6380771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:18:38+00:00 2026-05-25T02:18:38+00:00

How can you shrink a LinkedHashMap ? I overrode the removeEldestEntry method, but this

  • 0

How can you shrink a LinkedHashMap? I overrode the removeEldestEntry method, but this method is only called once when a new value is inserted. So there is no change of making the map smaller this way.

The LinkedHashMap only gives my a normal Iterator and doesn’t have any removeLast or listIterator method, so how can you find the last, say 1000, entries and remove them?

The only way I can think of is iterating through that whole thing. But that can take ages…

Creating a new map every time I want to remove only a few elements will also destroy the memory.

Maybe remove the first values of the Iterator and then reinserted them, when the maxSize was reduced in the removeEldestEntry method. Then the reinserting would kick out the oldest values. This is very ugly code… Any better ideas?

EDIT: Sry the iteration order is oldest to youngest. So it’s easy

  • 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-05-25T02:18:39+00:00Added an answer on May 25, 2026 at 2:18 am

    The iterator will iterate from oldest to youngest for LinekdHashMap. You if you want to shrink the LinkedHashMap to a size you can use the following.

    Map<K,V> lhm =
    int desiredSize = 
    for(Iterator iter = lhm.keySet().iterator();iter.hasNext()) {
       if(lhm.size() <= desiredSize) break;
       iter.next();     //required else IllegalStateException since current=null 
       iter.remove();
    }
    

    This should take about 20 ns per entry removed.

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

Sidebar

Related Questions

I'm trying to shrink this down... but can't figure out a way to do
We can set our DataBase read-only, and it restricts some commands like Auto-Shrink, Auto
Is there anyway which I can shrink ( like gzip ) a post data
Is there any way I can force a floated div to shrink instead of
This must be an easy question but I can't find a properly answer to
I want button in vertically, for that i can extend the height and shrink
Can a LINQ enabled app run on a machine that only has the .NET
I want to have an EC2 based cluster that can grow and shrink at
I'm having this same problem : How can I truncate a VARCHAR to the
Possible Duplicate: In PHP, can you instantiate an object and call a method on

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.