class LRU {
constructor(capacity) {
this.capacity = capacity;
this.map = new Map()
}
get(key) {
let val = this.map.get(key);
if (val === "undefined") return -1;
this.map.delete(key);
this.map.set(key, val)
}
put(key, val) {
if (this.map.has(key)) {
this.map.delete(key);
}
this.map.set(key, val)
let keys = this.map.keys()
while (this.capacity < this.map.size) {
this.map.delete(keys.next().value)
}
}
}
let lru = new LRU(3)
lru.put(1, "y")
lru.put(2, "z")
lru.put(3, "x")
lru.put(4, "yx")
lru.get(2)
lru.put(8, "u")
console.log(lru);