使用Java中的Map实现的队列数据结构,大小限制为5 [英] Data Structure for Queue using Map Implementations in Java with Size limit of 5
本文介绍了使用Java中的Map实现的队列数据结构,大小限制为5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些记录的地图.我想将该地图限制为仅5个元素,并且每当添加新元素时,都应删除第一项,并在地图的最后位置添加新元素.类似于FIFO.谁能建议我一个要使用的数据结构或解决方案本身.
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
方法.
Take LinkedHashMap as the base class and override its removeEldestEntry
method as described in the sample use of the method.
这篇关于使用Java中的Map实现的队列数据结构,大小限制为5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文