This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedHashMap - trying to use the method removeEldestEntry

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program compiles but without using "removeEldestEntry", I am not sure if it should be inheriting a size() from HashMap, or whether I need to implement one, and if so what it should contain.
Perhaps I also need a different constructor for the LinkedHashMap. I have searched the ranch and web I have not found a clear example.

I think it should get called automatically from the put method on line 21.



Many thanks!
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've worked it out - though not 100% clear as to how it works. It appears the method needs to be within the constructor of LinkedHashMap.
Also the third argument in the constructor pertaining to the sort order has to be false.

Working version that keeps the last five entries only:



Any further tips appreciated! thanks!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic