-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUCache.java
60 lines (45 loc) · 1.86 KB
/
LRUCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.tis.emo;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.logging.Logger;
public class LRUCache < K, V > extends LinkedHashMap < K, V > {
private int capacity; // Maximum number of items in the cache.
private static Logger _logger = Logger.getLogger(LRUCache.class.getName());
public LRUCache(int capacity) {
super(capacity+1, 1.0f, true); // Pass 'true' for accessOrder.
this.capacity = capacity;
}
protected boolean removeEldestEntry(Entry entry) {
return (size() > this.capacity);
}
public static void main(String[] args) {
_logger.info("setting value for gache ");
LRUCache cache = new LRUCache<>(5);
cache.put(1, "sample1");
_logger.info("setting value for gache cache.put(1, \"sample1\");");
cache.put(2, "sample1");
_logger.info("setting value for gache cache.put(2, \"sample2\");");
cache.put(3, "sample1");
_logger.info("setting value for gache cache.put(3, \"sample3\");");
cache.put(4, "sample1");
_logger.info("setting value for gache cache.put(4, \"sample4\");");
cache.put(5, "sample1");
_logger.info("setting value for gache cache.put(5, \"sample5\");");
_logger.info(cache+ "---> This is the value of the cache");
_logger.info(cache+ "---> This is the value of the cache");
cache.put(6, "sample6");
_logger.info("setting value for gache cache.put(6, \"sample6\");");
cache.put(7, "sample7");
_logger.info("setting value for gache cache.put(6, \"sample6\");");
_logger.info(cache+ "---> "
+ "------> This is the value of the cache"
+ "------>"
+ "------>"
+ "------>"
+ "------>"
+ "------>"
+ "------>"
);
_logger.info(cache.get(1)+" thisis getting 1 Key ");
}
}