java - 使用 Java 中的映射实现的队列数据结构,大小限制为 5

标签 java data-structures dictionary queue linkedhashmap

I have map with some records. I want to restrict that map to only 5 elements and whenever a new element is added the first item should be removed and new element should be added in last position of map. Something similar to FIFO. Can anyone please suggest me a data structure to use or the solution itself.

例如:

Map<String,String> map=new LinkedHashMap<String,String>(5);
for(int i=0;i<5;i++){
map.put(i+"",i+"");
}
map.put("5","5"); /* should remove map.get(0) and map.size will be still 5.Contents      would 1,2,3,4,5 */

最佳答案

LinkedHashMap作为基类并重写其 removeEldestEntry 方法,如该方法的示例使用中所述。

关于java - 使用 Java 中的映射实现的队列数据结构,大小限制为 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302635/

相关文章:

c - 测试链表

c - C 语言中如下所示的数组声明的用例是什么(这是有效的,不要说它是错误的)

python - 遍历字典或其键并更改值是不好的做法吗?

java - 停止来自第三方 Jars 的 log4j 调试打印

java - 将列表中的元素添加到字符串数组

Java如何检查字符串值是否是给定Class<?>的类型

algorithm - 有效检测与给定点对应的矩形区域( map )

ios - Swift iOS - 如何动态地将属性添加到现有类然后访问它们

python - 将 python 字典追加到列表中

java - 执行错误,从org.apache.hadoop.hive.ql.exec.FunctionTask返回代码1