MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / put

Method put

java/Linked Lists/LRUCache.java:45–60  ·  view source on GitHub ↗
(int key, int val)

Source from the content-addressed store, hash-verified

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;

Calls 3

removeMethod · 0.95
addToTailMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected