美文网首页
固定长度的Map(采用LRU策略)的实现

固定长度的Map(采用LRU策略)的实现

作者: zhglance | 来源:发表于2019-02-14 16:57 被阅读4次
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class FixedCapacityMap {

 protected static Map<Integer, Long> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());

 public static Long get(int key) {
   return fixedCapacityCache.get(key);
 }

 public static void put(int key, long value) {
   fixedCapacityCache.put(key, value);
 }

 public static int size() {
   return fixedCapacityCache.size();
 }


 static class LRUHashMap<K, V> extends LinkedHashMap<K, V> {
   private int capacity = 50;
   @Override
   protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
     return size() > capacity;
   }

 }


}

相关文章

网友评论

      本文标题:固定长度的Map(采用LRU策略)的实现

      本文链接:https://www.haomeiwen.com/subject/rgzeeqtx.html