返回信息流参考百度知道上的这篇
http://zhidao.baidu.com/question/132889967.html
把代码原封不动拷过去的
-----------------------------------
LinkedHashMap lhm = new LinkedHashMap(6);
for (int i = 0; i < 6; i++) {
lhm.put(i, i);
}
System.out.println(lhm);
lhm.get(2);
System.out.println(lhm);
lhm.get(2);
System.out.println(lhm);
-----------------------------------
理论上结果应该是
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5}
{0=0, 1=1, 3=3, 4=4, 5=5, 2=2}
{0=0, 1=1, 3=3, 4=4, 5=5, 2=2}
可是我的结果还是
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5}
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5}
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5}
完全没有变 这是怎么回事呀? 求教
这是一条镜像帖。来源:北邮人论坛 / java / #19174同步于 2011/6/30
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
【求助】LinkedHashMap中的LRU算法为什么没有改变排序结果
JC
2011/6/30镜像同步2 回复
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
解决了。。
按照这种方式实例化即可:LinkedHashMap lhm= new LinkedHashMap(6, 0.75f, true);
注意最后一个参数很重要。 表明了你关注的是插入顺序还是访问顺序。看他的API说明。
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder)构造一个带指定初始容量、加载因子和排序模式的空 LinkedHashMap 实例。
参数:
initialCapacity - 初始容量
loadFactor - 加载因子
accessOrder - 排序模式 - 对于访问顺序,为 true;对于插入顺序,则为 false
LinkedHashMap lhm = new LinkedHashMap(6,6,true);
/**
* Constructs an empty <tt>LinkedHashMap</tt> instance with the
* specified initial capacity, load factor and ordering mode.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @param accessOrder the ordering mode - <tt>true</tt> for
* access-order, <tt>false</tt> for insertion-order
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}