我正在阅读 JDK 11 中的 LinkedHashMap 源代码,我发现了一段死代码(我不确定)
众所周知,LinkedHashMap 使用双向链表来保存所有元素的顺序。它有一个成员叫做 accessOrder
final boolean accessOrder;
Run Code Online (Sandbox Code Playgroud)
默认情况下它是 false,但如果它被设置为 true,每次运行时get,它都会将它到达的元素移动到链表的末尾。这就是函数afterNodeAccess所做的。
//if accessOrder were set as true, after you visit node e, if e is not the end node of the linked list,
//it will move the node to the end of the linkedlist.
void afterNodeAccess(Node<K, V> e) {
LinkedHashMap.Entry<K, V> last;
if(accessOrder && (last = tail) != e) {
//if enter `if` ?it indicates that e is not the end of …Run Code Online (Sandbox Code Playgroud)