-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLruCache.java
More file actions
59 lines (58 loc) · 1.95 KB
/
LruCache.java
File metadata and controls
59 lines (58 loc) · 1.95 KB
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
package hashMapTesting;
import java.util.*;
// Do not edit the class below except for the insertKeyValuePair,
// getValueFromKey, and getMostRecentKey methods.
// Feel free to add new properties and methods to the class.
public class LruCache {
static class LRUCache {
int maxSize;
String mostRecentlyUsed;
HashMap<String,Integer> cache = new HashMap<>();
ArrayList<String> store = new ArrayList<>();
public LRUCache(int maxSize) {
this.maxSize = maxSize > 1 ? maxSize : 1;
}
public void insertKeyValuePair(String key, int value) {
// Write your code here.
if(cache.size() < maxSize){
store.add(key);
cache.put(key,value);
}else if(cache.size() == maxSize){
if(cache.containsKey(key)){
int val = cache.get(key);
cache.replace(key,val,value);
store.remove(key);
store.add(key);
}else{
String oldKey = store.get(0);
store.remove(oldKey);
store.add(key);
cache.remove(oldKey);
cache.put(key,value);
}
}
}
public LRUResult getValueFromKey(String key) {
// Write your code here.
if(cache.containsKey(key)){
store.remove(key);
store.add(key);
return new LRUResult (true,cache.get(key));
}else{
return new LRUResult(false,null);
}
}
public String getMostRecentKey() {
// Write your code here.
return store.get(store.size()-1);
}
}
static class LRUResult {
boolean found;
Integer value;
public LRUResult(boolean found, Integer value) {
this.found = found;
this.value = value;
}
}
}