Last active
          December 27, 2015 18:39 
        
      - 
      
 - 
        
Save skdangi/7371466 to your computer and use it in GitHub Desktop.  
    LRUCacheImplementation
  
        
  
    
      This file contains hidden or 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.HashMap; | |
| import java.util.Map; | |
| public class LRUCache { | |
| private static class Database { | |
| private Map<Integer, Integer> map = new HashMap<Integer, Integer>() { | |
| { | |
| put(1, 1); | |
| put(2, 2); | |
| put(3, 3); | |
| put(4, 4); | |
| put(5, 5); | |
| } | |
| }; | |
| public int read(int key) { | |
| return map.get(key); | |
| } | |
| } | |
| private static class Entry { | |
| int value; | |
| Entry succ; | |
| Entry prev; | |
| public Entry() { | |
| } | |
| public Entry(int value) { | |
| this.value = value; | |
| } | |
| public void addFirst(Entry temp) { | |
| this.succ.prev = temp; | |
| temp.prev = this; | |
| temp.succ = this.succ; | |
| this.succ = temp; | |
| } | |
| public void removeLast() { | |
| Entry.remove(this.prev); | |
| } | |
| public static void remove(Entry temp) { | |
| temp.prev.succ = temp.succ; // delete node | |
| temp.prev.succ.prev = temp.prev; | |
| } | |
| public void print() { | |
| Entry index = this.succ; | |
| while (index != this) { | |
| System.out.print(index.value + " "); | |
| index = index.succ; | |
| } | |
| System.out.println(); | |
| } | |
| public static Entry newInstance() { | |
| Entry temp = new Entry(); | |
| temp.prev = temp; | |
| temp.succ = temp; | |
| return temp; | |
| } | |
| } | |
| Database database; | |
| int size; | |
| int currentFill; | |
| HashMap<Integer, Entry> map; | |
| Entry list; | |
| public LRUCache(Database database, int size) { | |
| this.database = database; | |
| this.size = size; | |
| map = new HashMap<Integer, Entry>(); | |
| list = Entry.newInstance(); | |
| } | |
| public int get(int key) { | |
| Entry temp = map.get(key); | |
| if (null == temp) { | |
| put(key, database.read(key)); | |
| }else{ | |
| Entry.remove(temp); | |
| list.addFirst(temp); | |
| } | |
| //list.print(); | |
| return map.get(key).value; | |
| } | |
| public void put(int key, int value) { | |
| Entry temp = map.get(key); | |
| if (null == temp) { | |
| temp = new Entry(value); | |
| if (size == currentFill) { | |
| list.removeLast(); | |
| currentFill--; | |
| } | |
| map.put(key, temp); | |
| currentFill++; | |
| } else { | |
| temp.value = value;// update value | |
| Entry.remove(temp); | |
| } | |
| list.addFirst(temp); | |
| //list.print(); | |
| } | |
| public static void main(String[] args) { | |
| Database database = new Database(); | |
| LRUCache cache = new LRUCache(database, 3); | |
| for (int i = 1; i <= 5; i++) { | |
| cache.get(i); | |
| } | |
| cache.get(4); | |
| cache.get(4); | |
| cache.put(5,5); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment