(int key, int val)
| 43 | } |
| 44 | |
| 45 | public void put(int key, int val) { |
| 46 | // If a node with this key already exists, remove it from the |
| 47 | // linked list. |
| 48 | if (cache.containsKey(key)) { |
| 49 | remove(cache.get(key)); |
| 50 | } |
| 51 | DoublyLinkedListNode node = new DoublyLinkedListNode(key, val); |
| 52 | cache.put(key, node); |
| 53 | // Remove the least recently used node from the cache if adding |
| 54 | // this new node will result in an overflow. |
| 55 | if (cache.size() > capacity) { |
| 56 | cache.remove(head.next.key); |
| 57 | remove(head.next); |
| 58 | } |
| 59 | addToTail(node); |
| 60 | } |
| 61 | |
| 62 | private void addToTail(DoublyLinkedListNode node) { |
| 63 | DoublyLinkedListNode prevNode = tail.prev; |
no test coverage detected