(int key, int value)
| 28 | } |
| 29 | |
| 30 | public void put(int key, int value) { |
| 31 | if (cache.containsKey(key)) { |
| 32 | remove(cache.get(key)); |
| 33 | } |
| 34 | cache.put(key, new Node(key, value)); |
| 35 | insert(cache.get(key)); |
| 36 | |
| 37 | if (cache.size() > capacity) { |
| 38 | // remove from the list and delte the LRU from the hashmap |
| 39 | Node lru = this.left.next; |
| 40 | remove(lru); |
| 41 | cache.remove(lru.key); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // remove node from list |
| 46 | public void remove(Node node) { |