Skip to content

Instantly share code, notes, and snippets.

@vinodjayachandran
Created October 26, 2023 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodjayachandran/a213f71170ec5b7782c253c91cfa984f to your computer and use it in GitHub Desktop.
Save vinodjayachandran/a213f71170ec5b7782c253c91cfa984f to your computer and use it in GitHub Desktop.
Implementation of LRUCache using LinkedHashMap
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