lru cache java

http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html

acm之家的讲解是在是好,丰富

import java.util.LinkedHashMap;
public class LRUCache {
     private int capacity;
     private LinkedHashMap<Integer,Integer> map=null;

    public LRUCache(int capacity) {
        this.capacity=capacity;
        map=new LinkedHashMap<Integer,Integer>(16,0.75f,true){
            protected boolean removeEldestEntry(java.util.Map.Entry<Integer,Integer> eldest) {  

            return size()>LRUCache.this.capacity;

        }

    };
    }

    public int get(int key) {
        if(map.get(key)==null)
        {
            return -1;
        }
        return map.get(key);

    }

    public void set(int key, int value) {
        map.put(key,value);

    }
}

  

lru cache java

时间: 2024-10-10 20:58:00

lru cache java的相关文章

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

LeetCode – LRU Cache (Java)

Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return

Java for LeetCode 146 LRU Cache 【HARD】

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

A LRU Cache in 10 Lines of Java

I had a couple of interviews long ago which asked me to implemented a least recently used (LRU) cache. A cache itself can simply be implemented using a hash table, however adding a size limit gives an interesting twist on the question. Let's take a l

LRU Cache leetcode java

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

[Leetcode][JAVA] LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(

java基于Hash表和双向链表简单实现LRU Cache

package lru; import java.util.HashMap; public class LRUCache2<K,V> { public final int capacity; class DNode{ K key; V value; DNode next; DNode pre; public DNode(K key, V value, LRUCache2<K, V>.DNode pre, LRUCache2<K, V>.DNode next) { sup

LeetCode——LRU Cache

Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise r

LRU算法 - LRU Cache

这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应用在缓存替换策略中.其中的”使用”包括访问get和更新set. LRU算法 LRU是Least Recently Used 近期最少使用算法.内存管理的一种页面置换算法,对于在内存中但又不用的数据快(内存块)叫做LRU,Oracle会根据那些数据属于LRU而将其移出内存而腾出空间来加载另外的数据,一