Guest User

Untitled

a guest
Mar 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class LRUCache {
  2. class LRUMap<K, V> extends LinkedHashMap<K, V>{
  3. int size;
  4.  
  5. public LRUMap(int size) {
  6. super(size, 0.75f, true);
  7. this.size = size;
  8. }
  9.  
  10. @Override
  11. protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
  12. return size() > size;
  13. }
  14. }
  15.  
  16. LRUMap<Integer, Integer> map;
  17.  
  18. public LRUCache(int capacity) {
  19. map = new LRUMap<>(capacity);
  20. }
  21.  
  22. public int get(int key) {
  23. return map.getOrDefault(key, -1);
  24. }
  25.  
  26. public void put(int key, int value) {
  27. map.put(key, value);
  28. }
  29. }
Add Comment
Please, Sign In to add comment