Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Implementing a Least-Recently-Used (LRU) Cache

import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; public class Main {   public static void main(String[] argv) throws Exception {     final int MAX_ENTRIES = 100;     Map cache = new LinkedHashMap(MAX_ENTRIES + 1, .75F, true) {       public boolean removeEldestEntry(Map.Entry eldest) {         return size() > MAX_ENTRIES;       }     };     Object key = "key";     Object value = "value";     cache.put(key, value);     Object o = cache.get(key);     if (o == null && !cache.containsKey(key)) {     }     cache = (Map) Collections.synchronizedMap(cache);   } }