本文介绍了使用Java中的Map实现的队列数据结构,大小限制为5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 10:50