10
10
public class LFUCache <K , V > {
11
11
12
12
private class Node {
13
-
14
- private K key ;
13
+ private final K key ;
15
14
private V value ;
16
15
private int frequency ;
17
16
private Node previous ;
@@ -26,67 +25,67 @@ private class Node {
26
25
27
26
private Node head ;
28
27
private Node tail ;
29
- private Map <K , Node > map = null ;
30
- private Integer capacity ;
28
+ private final Map <K , Node > cache ;
29
+ private final int capacity ;
31
30
private static final int DEFAULT_CAPACITY = 100 ;
32
31
33
32
public LFUCache () {
34
- this . capacity = DEFAULT_CAPACITY ;
33
+ this ( DEFAULT_CAPACITY ) ;
35
34
}
36
35
37
- public LFUCache (Integer capacity ) {
36
+ public LFUCache (int capacity ) {
37
+ if (capacity <= 0 ) {
38
+ throw new IllegalArgumentException ("Capacity must be greater than zero." );
39
+ }
38
40
this .capacity = capacity ;
39
- this .map = new HashMap <>();
41
+ this .cache = new HashMap <>();
40
42
}
41
43
42
44
/**
43
- * This method returns value present in the cache corresponding to the key passed as parameter
45
+ * Retrieves the value for the given key from the cache. Increases the frequency of the node.
44
46
*
45
- * @param <K> key for which value is to be retrieved
46
- * @returns <V> object corresponding to the key passed as parameter, returns null if <K> key is
47
- * not present in the cache
47
+ * @param key The key to look up.
48
+ * @return The value associated with the key, or null if the key is not present.
48
49
*/
49
50
public V get (K key ) {
50
- if (this .map .get (key ) == null ) {
51
+ Node node = cache .get (key );
52
+ if (node == null ) {
51
53
return null ;
52
54
}
53
-
54
- Node node = map .get (key );
55
55
removeNode (node );
56
56
node .frequency += 1 ;
57
57
addNodeWithUpdatedFrequency (node );
58
-
59
58
return node .value ;
60
59
}
61
60
62
61
/**
63
- * This method stores <K> key and <V> value in the cache
62
+ * Adds or updates a key-value pair in the cache. If the cache is full, the least frequently used item is evicted.
64
63
*
65
- * @param <K> key which is to be stored in the cache
66
- * @param <V> value which is to be stored in the cache
64
+ * @param key The key to insert or update.
65
+ * @param value The value to insert or update.
67
66
*/
68
67
public void put (K key , V value ) {
69
- if (map .containsKey (key )) {
70
- Node node = map .get (key );
68
+ if (cache .containsKey (key )) {
69
+ Node node = cache .get (key );
71
70
node .value = value ;
72
71
node .frequency += 1 ;
73
72
removeNode (node );
74
73
addNodeWithUpdatedFrequency (node );
75
74
} else {
76
- if (map .size () >= capacity ) {
77
- map .remove (this .head .key );
75
+ if (cache .size () >= capacity ) {
76
+ cache .remove (this .head .key );
78
77
removeNode (head );
79
78
}
80
79
Node node = new Node (key , value , 1 );
81
80
addNodeWithUpdatedFrequency (node );
82
- map .put (key , node );
81
+ cache .put (key , node );
83
82
}
84
83
}
85
84
86
85
/**
87
- * This method stores the node in the cache with updated frequency
86
+ * Adds a node to the linked list in the correct position based on its frequency.
88
87
*
89
- * @param Node node which is to be updated in the cache
88
+ * @param node The node to add.
90
89
*/
91
90
private void addNodeWithUpdatedFrequency (Node node ) {
92
91
if (tail != null && head != null ) {
@@ -123,9 +122,9 @@ private void addNodeWithUpdatedFrequency(Node node) {
123
122
}
124
123
125
124
/**
126
- * This method removes node from the cache
125
+ * Removes a node from the linked list.
127
126
*
128
- * @param Node node which is to be removed in the cache
127
+ * @param node The node to remove.
129
128
*/
130
129
private void removeNode (Node node ) {
131
130
if (node .previous != null ) {
0 commit comments