From 4435076f73cb2f611ad6a8ad01faf661bb6f14d8 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Tue, 16 Oct 2018 22:02:49 +0800 Subject: [PATCH] auto commit --- notes/缓存.md | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/notes/缓存.md b/notes/缓存.md index 3a3e8140..0a700d21 100644 --- a/notes/缓存.md +++ b/notes/缓存.md @@ -88,14 +88,12 @@ public class LRU implements Iterable { public void put(K key, V value) { - Node node; if (map.containsKey(key)) { - node = map.get(key); + Node node = map.get(key); unlink(node); } - if (node == null) { - node = new Node(key, value); - } + + Node node = new Node(key, value); map.put(key, node); appendHead(node); @@ -107,28 +105,38 @@ public class LRU implements Iterable { private void unlink(Node node) { + Node pre = node.pre; Node next = node.next; + pre.next = next; next.pre = pre; - + node.pre = null; node.next = null; } private void appendHead(Node node) { - node.next = head.next; - node.next.pre = node; + Node next = head.next; + node.next = next; + next.pre = node; node.pre = head; head.next = node; } private Node removeTail() { + Node node = tail.pre; - tail.pre = node.pre; - node.pre.next = tail; + + Node pre = node.pre; + tail.pre = pre; + pre.next = tail; + + node.pre = null; + node.next = null; + return node; } @@ -138,6 +146,7 @@ public class LRU implements Iterable { return new Iterator() { private Node cur = head.next; + @Override public boolean hasNext() { return cur != tail;