首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

关于LinkedHashMap的一些简略实验

2012-09-05 
关于LinkedHashMap的一些简单实验同为实现了Map接口的类,LinedHashMap在迭代的时候可以保证元素插入的顺序

关于LinkedHashMap的一些简单实验
同为实现了Map接口的类,LinedHashMap在迭代的时候可以保证元素插入的顺序,并且可以按照访问的次数,从近期访问最少到近期访问最多的顺序(LRU)来排序.以下是自己写的一些简单的实验代码,供自己备份

public class Test {   public static void main(String args[])   {          //构造函数中,第三个参数是排序的模式,对于访问顺序为true,对于插入顺序为false;   Map<Integer,Integer> map=new LinkedHashMap<Integer,Integer>(10,0.8f,true);   map.put(1, 1);   map.put(3, 9);   map.put(2, 989);   map.put(4, 77);   for(Map.Entry<Integer,Integer> entry:map.entrySet())   {   System.out.println(entry.getValue());   }   map.get(1);map.get(3);map.get(4);map.get(4);   System.out.println("------------------------------");   for(Map.Entry<Integer,Integer> entry:map.entrySet())   {   System.out.println(entry.getValue());   }      }}


热点排行