[英]Dictionary or similar data structure that only stores the most recent n entries?
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. 我想知道是否有一个内置的Java数据结构,其行为类似于字典, 但仅保留固定数量的最新条目。 So I could keep the size more-or-less constant but also have it behave like a dictionary or hashmap. 因此,我可以将大小或多或少保持恒定,但也可以使其表现得像字典或哈希图。
You could create one quite easily by extending LinkedHashMap. 您可以通过扩展LinkedHashMap轻松地创建一个。 When adding an entry, check the size and remove the oldest item if already at maximum size. 添加条目时,检查尺寸并删除最旧的项目(如果已经达到最大尺寸)。
我认为LRUMap将适合您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.