I'm wondering if there's a built-in Java data structure that behaves like a dictionary but only keeps a fixed number of recent entries. So I could keep the size more-or-less constant but also have it behave like a dictionary or hashmap.
Asked
Active
Viewed 112 times
1
-
So you want a keyed cache, managed on something like an LRU replacement basis. – Hot Licks Feb 12 '13 at 20:38
-
Have a look at this post: http://stackoverflow.com/a/224886/702638 – Hunter McMillen Feb 12 '13 at 20:40
-
possible duplicate of [Easy, simple to use LRU cache in java](http://stackoverflow.com/questions/224868/easy-simple-to-use-lru-cache-in-java) – Jon Skeet Feb 12 '13 at 20:42
2 Answers
1
You could create one quite easily by extending LinkedHashMap. When adding an entry, check the size and remove the oldest item if already at maximum size.

Patricia Shanahan
- 25,849
- 4
- 38
- 75