image.png
    实际上底层维护双向链表和哈希表,将新查询的数据或插入的数据移动到链表头,若再插入数据时容量不足则从链表尾部删除

    1. class LRUCache {
    2. Map<Integer, Node> map;
    3. DoubleLinkedList cache;
    4. int capacity;
    5. public LRUCache(int capacity) {
    6. this.capacity = capacity;
    7. map = new HashMap<>();
    8. cache = new DoubleLinkedList();
    9. }
    10. public int get(int key) {
    11. if (!map.containsKey(key)) {
    12. return -1;
    13. }
    14. int val = map.get(key).value;
    15. put(key, val);
    16. return val;
    17. }
    18. public void put(int key, int value) {
    19. Node newNode = new Node(key, value);
    20. if (map.containsKey(key)) {
    21. cache.delete(map.get(key));
    22. map.put(key, newNode);
    23. cache.addFirst(newNode);
    24. } else {
    25. if (capacity == map.size()) {
    26. int k = cache.deleteLast(newNode);
    27. map.remove(k);
    28. }
    29. cache.addFirst(newNode);
    30. map.put(key, newNode);
    31. }
    32. }
    33. class Node {
    34. int key;
    35. int value;
    36. Node prev;
    37. Node next;
    38. public Node(int key, int value) {
    39. this.key = key;
    40. this.value = value;
    41. }
    42. }
    43. class DoubleLinkedList {
    44. Node head;
    45. Node tail;
    46. public DoubleLinkedList() {
    47. head = new Node(0, 0);
    48. tail = new Node(0, 0);
    49. head.next = tail;
    50. tail.prev = head;
    51. }
    52. public void addFirst(Node node) {
    53. node.next = head.next;
    54. node.prev = head;
    55. head.next.prev = node;
    56. head.next = node;
    57. }
    58. public int delete(Node node) {
    59. int key = node.key;
    60. node.prev.next = node.next;
    61. node.next.prev = node.prev;
    62. return key;
    63. }
    64. public int deleteLast(Node node) {
    65. if (tail.prev == head) return -1;
    66. return delete(tail.prev);
    67. }
    68. }
    69. }