Skip to content

Commit 54cb082

Browse files
committed
🐛 Fixing a bug. LinkedHashMap
1 parent e518cb0 commit 54cb082

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

MD/collection/LinkedHashMap.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,24 @@ LinkedHashMap 的 `get()` 方法也重写了:
238238
if (e == null)
239239
return null;
240240

241-
//多了一个判断是否是按照访问顺序排序,是则将当前的 Entry 移动到链表末尾
241+
//多了一个判断是否是按照访问顺序排序,是则将当前的 Entry 移动到链表头部。
242242
e.recordAccess(this);
243243
return e.value;
244244
}
245+
246+
void recordAccess(HashMap<K,V> m) {
247+
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
248+
if (lm.accessOrder) {
249+
lm.modCount++;
250+
251+
//删除
252+
remove();
253+
//添加到头部
254+
addBefore(lm.header);
255+
}
256+
}
257+
258+
245259
```
246260

247261
`clear()` 清空就要比较简单了:

0 commit comments

Comments
 (0)