Skip to content

Commit 6982be3

Browse files
authored
Merge branch 'master' into test/LinkedListStackTest
2 parents 9044a13 + 844aeaf commit 6982be3

File tree

1 file changed

+26
-27
lines changed
  • src/main/java/com/thealgorithms/datastructures/caches

1 file changed

+26
-27
lines changed

src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
public class LFUCache<K, V> {
1111

1212
private class Node {
13-
14-
private K key;
13+
private final K key;
1514
private V value;
1615
private int frequency;
1716
private Node previous;
@@ -26,67 +25,67 @@ private class Node {
2625

2726
private Node head;
2827
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;
3130
private static final int DEFAULT_CAPACITY = 100;
3231

3332
public LFUCache() {
34-
this.capacity = DEFAULT_CAPACITY;
33+
this(DEFAULT_CAPACITY);
3534
}
3635

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+
}
3840
this.capacity = capacity;
39-
this.map = new HashMap<>();
41+
this.cache = new HashMap<>();
4042
}
4143

4244
/**
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.
4446
*
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.
4849
*/
4950
public V get(K key) {
50-
if (this.map.get(key) == null) {
51+
Node node = cache.get(key);
52+
if (node == null) {
5153
return null;
5254
}
53-
54-
Node node = map.get(key);
5555
removeNode(node);
5656
node.frequency += 1;
5757
addNodeWithUpdatedFrequency(node);
58-
5958
return node.value;
6059
}
6160

6261
/**
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.
6463
*
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.
6766
*/
6867
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);
7170
node.value = value;
7271
node.frequency += 1;
7372
removeNode(node);
7473
addNodeWithUpdatedFrequency(node);
7574
} else {
76-
if (map.size() >= capacity) {
77-
map.remove(this.head.key);
75+
if (cache.size() >= capacity) {
76+
cache.remove(this.head.key);
7877
removeNode(head);
7978
}
8079
Node node = new Node(key, value, 1);
8180
addNodeWithUpdatedFrequency(node);
82-
map.put(key, node);
81+
cache.put(key, node);
8382
}
8483
}
8584

8685
/**
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.
8887
*
89-
* @param Node node which is to be updated in the cache
88+
* @param node The node to add.
9089
*/
9190
private void addNodeWithUpdatedFrequency(Node node) {
9291
if (tail != null && head != null) {
@@ -123,9 +122,9 @@ private void addNodeWithUpdatedFrequency(Node node) {
123122
}
124123

125124
/**
126-
* This method removes node from the cache
125+
* Removes a node from the linked list.
127126
*
128-
* @param Node node which is to be removed in the cache
127+
* @param node The node to remove.
129128
*/
130129
private void removeNode(Node node) {
131130
if (node.previous != null) {

0 commit comments

Comments
 (0)