关于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()); } }}