### 需求背景：

        int initialCapacity = 10;//初始化容量
float loadFactor = 0.75f;//加载因子，一般是 0.75f
boolean accessOrder = true;//排序方式 false 基于插入顺序  true  基于访问顺序
Map<String, Integer> map = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);

   for (int i = 0; i < 10; i++) {
map.put(String.valueOf(i), i);
}
//访问前顺序
for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Integer> next = it.next();
Log.e(TAG, "linkedMap--before-->" + next.getKey());
}

//模拟访问
map.get("5");

//访问后数据
for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Integer> next = it.next();
Log.e(TAG, "linkedMap--after-->" + next.getKey());
}
}

       final int initialCapacity = 10;//初始化容量
float loadFactor = 0.75f;//加载因子，一般是 0.75f
boolean accessOrder = true;//排序方式 false 基于插入顺序  true  基于访问顺序
//Map<String, Integer> map = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
Map<String, Integer> map = new LinkedHashMap(initialCapacity, loadFactor, accessOrder) {

@Override
protected boolean removeEldestEntry(Entry eldest) {
return size() > initialCapacity;
}
};
for (int i = 0; i < 15; i++) {
map.put(String.valueOf(i), i);
}
//访问前顺序
for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Integer> next = it.next();
Log.e(TAG, "linkedMap--before-->" + next.getKey());
}

posted on 2016-07-31 08:45  总李写代码  阅读(5141)  评论(5编辑  收藏