Created
October 26, 2023 11:57
-
-
Save vinodjayachandran/a213f71170ec5b7782c253c91cfa984f to your computer and use it in GitHub Desktop.
Implementation of LRUCache using LinkedHashMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedHashMap; | |
import java.util.Map; | |
public class LRUCache { | |
int capacity; | |
LinkedHashMap<String,String> cache; | |
public LRUCache(int capacity) { | |
this.capacity = capacity; | |
this.cache = new LinkedHashMap<>(capacity,0.75f, true) { | |
@Override | |
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) { | |
return size() > capacity; | |
} | |
}; | |
} | |
public String get(String key){ | |
return cache.getOrDefault(key,null); | |
} | |
public String put(String key, String value){ | |
return cache.put(key,value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment