Skip to content

Commit 836ec5f

Browse files
committed
💬 update LinkedHashMap
1 parent 9406221 commit 836ec5f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

MD/collection/LinkedHashMap.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,18 @@
182182
}
183183
```
184184

185-
主体的实现都是借助于 `HashMap` 来完成的,只是对其中的 `recordAccess(),createEntry(), createEntry()` 进行了重写。
185+
主体的实现都是借助于 `HashMap` 来完成的,只是对其中的 `recordAccess(),createEntry(), createEntry()` 进行了重写。
186+
187+
`LinkedHashMap` 的实现:
188+
189+
```java
190+
//就是判断是否是根据访问顺序排序,如果是则需要将当前这个 Entry 移动到链表的末尾
191+
void recordAccess(HashMap<K,V> m) {
192+
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
193+
if (lm.accessOrder) {
194+
lm.modCount++;
195+
remove();
196+
addBefore(lm.header);
197+
}
198+
}
199+
```

0 commit comments

Comments
 (0)