Skip to content

Commit bcd6b13

Browse files
authored
Update LRU.java
PR review changes
1 parent 09f7cdc commit bcd6b13

File tree

1 file changed

+20
-26
lines changed
  • src/main/java/dataStructures/lruCache

1 file changed

+20
-26
lines changed

src/main/java/dataStructures/lruCache/LRU.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
/**
66
* Implementation of Least Recently Used (LRU) Cache
77
*
8+
* @param <K> generic type of key to be stored
9+
* @param <V> generic type of associated value corresponding to a given key
810
* Constructor:
911
* LRU(int capacity)
1012
* Client methods:
1113
* get(K key)
1214
* put(K key, V value)
13-
* Both methods above run in O(1) average time complexity
15+
* Both methods above run in expected O(1) time complexity
1416
*/
15-
class LRU {
17+
class LRU<K, V> {
1618
/**
1719
* Helper node class that implements doubly linked list
1820
*/
19-
private class doublyLinkedList {
20-
private int key;
21-
private int val;
22-
private doublyLinkedList next;
23-
private doublyLinkedList prev;
21+
private class doublyLinkedListNode<K, V> {
22+
private K key;
23+
private V val;
24+
private doublyLinkedListNode<K, V> next;
25+
private doublyLinkedListNode<K, V> prev;
2426
}
2527

26-
private doublyLinkedList dllHead;
27-
private doublyLinkedList dllTail;
28-
private HashMap<Integer, doublyLinkedList> keyToNode = new HashMap();
28+
private doublyLinkedListNode<K, V> dllHead;
29+
private doublyLinkedListNode<K, V> dllTail;
30+
private HashMap<K, doublyLinkedListNode<K, V>> keyToNode = new HashMap<>();
2931
private int capacity;
3032
private int lengthOfList = 0;
3133

@@ -37,31 +39,23 @@ private class doublyLinkedList {
3739
public LRU(int capacity) {
3840
this.capacity = capacity;
3941

40-
dllHead = new doublyLinkedList();
41-
dllHead.key = -1;
42-
dllHead.val = -1;
43-
44-
dllTail = new doublyLinkedList();
45-
dllTail.key = -1;
46-
dllTail.val = -1;
47-
48-
dllHead.prev = null;
42+
dllHead = new doublyLinkedListNode<>();
43+
dllTail = new doublyLinkedListNode<>();
4944
dllHead.next = dllTail;
5045
dllTail.prev = dllHead;
51-
dllTail.next = null;
5246
}
5347

5448
/**
5549
* Return the value of the key if it exists or return null
5650
*
5751
* @param key key of the value to be obtained from LRU cache
5852
*/
59-
public int get(int key) {
53+
public V get(K key) {
6054
if (!keyToNode.containsKey(key)) {
61-
return -1;
55+
return null;
6256
}
6357

64-
doublyLinkedList temp = keyToNode.get(key);
58+
doublyLinkedListNode<K, V> temp = keyToNode.get(key);
6559
temp.prev.next = temp.next;
6660
temp.next.prev = temp.prev;
6761

@@ -79,13 +73,13 @@ public int get(int key) {
7973
* @param key key of the value to be inserted to LRU cache
8074
* @param value value to be inserted to LRU cache
8175
*/
82-
public void put(int key, int value) {
76+
public void put(K key, V value) {
8377
boolean addingNewNode = true;
8478

85-
doublyLinkedList newlyCached;
79+
doublyLinkedListNode<K, V> newlyCached;
8680

8781
if (!keyToNode.containsKey(key)) {
88-
newlyCached = new doublyLinkedList();
82+
newlyCached = new doublyLinkedListNode<>();
8983
newlyCached.key = key;
9084
newlyCached.val = value;
9185
keyToNode.put(key, newlyCached);

0 commit comments

Comments
 (0)