LRU算法实现(Java版)

创建链表ListNode<K,V>类

/**
 * @author :linwl
 * @date :Created in 2019/11/7 14:52
 * @description :
 * @modified By:
 */
public class ListNode<K,V> {

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    public ListNode<K, V> getPrev() {
        return prev;
    }

    public void setPrev(ListNode<K, V> prev) {
        this.prev = prev;
    }

    public ListNode<K, V> getNext() {
        return next;
    }

    public void setNext(ListNode<K, V> next) {
        this.next = next;
    }
    
  @Override
  public String toString() {
        return "ListNode(key=" + this.getKey() + ", value=" + this.getValue() + ", prev=" + this.getPrev() + ", next=" + this.getNext() + ")";
    }

    /**
     * 索引key
     */
    private K key;

    /**
     * 数据
     */
    private V value;

    /**
     * 上一个节点
     */
    private ListNode<K,V> prev;

    /**
     * 下一个节点
     */
    private ListNode<K,V> next;

    public ListNode(K key,V value)
    {
        this.key =key;
        this.value =value;
    }

    public ListNode()
    {

    }
}

LRU算法实现类

 import com.linwl.lrudemo.entity.ListNode;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author :linwl
 * @date :Created in 2019/11/7 14:57
 * @description :
 * @modified By:
 */
public class LRUCache<V> {

    /**
     * 容量
     */
    private int capacity =1024;

    /**
     * 节点记录表
     */
    private Map<String, ListNode<String, V>> table = new ConcurrentHashMap<>();

    /**
     * 双向链表头部
     */
    private ListNode<String,V> head;

    /**
     * 双向链表尾部
     */
    private ListNode<String,V> tail;

    public LRUCache(int capacity)
    {
        this();
        this.capacity =capacity;
    }

    public LRUCache()
    {
        head =new ListNode<>();
        tail =new ListNode<>();
        head.setNext(tail);
        head.setPrev(null);
        tail.setPrev(head);
        tail.setNext(null);
    }


    public V get(String key)
    {
        ListNode<String,V> node = table.get(key);
       //缓存不存在
        if(node == null)
        {
            return null;
        }
        //如果存在,则需要移动Node节点到表头
        //截断node
        node.getPrev().setNext(node.getNext());
        node.getNext().setPrev(node.getPrev());
        //移动到表头
        node.setNext(head.getNext());
        node.setPrev(head);
        head.getNext().setPrev(node);
        head.setNext(node);
        table.put(key,node);
        return node.getValue();
    }

    public void put(String key,V value)
    {
        ListNode<String, V> node = table.get(key);
        //如果Node不在表中,代表缓存中并没有
        if (node == null) {
            if(table.size() == capacity)
            {
                //超容量了,移除尾部缓存
                table.remove(tail.getPrev().getKey());
                tail.setPrev(tail.getPrev().getPrev());
                tail.setNext(null);
            }
            node =new ListNode<>(key,value);
            table.put(key,node);
        }
        //移动Node节点到表头
        node.setNext(head.getNext());
        head.getNext().setPrev(node);
        node.setPrev(head);
        head.setNext(node);

    }
}

使用效果

 public static void main(String[] args)
    {
        LRUCache<ListNode> cache = new LRUCache<>(4);
        ListNode<String, Integer> node1 = new ListNode<>("key1", 1);
        ListNode<String, Integer> node2 = new ListNode<>("key2", 2);
        ListNode<String, Integer> node3 = new ListNode<>("key3", 3);
        ListNode<String, Integer> node4 = new ListNode<>("key4", 4);
        ListNode<String, Integer> node5 = new ListNode<>("key5", 5);
        ListNode<String, Integer> node6 = new ListNode<>("key6", 6);
        cache.put("key1", node1);
        cache.put("key2", node2);
        cache.put("key3", node3);
        cache.put("key4", node4);
        System.out.println(cache.get("key2"));
        cache.put("key5", node5);
        System.out.println(cache.get("key1"));
        cache.put("key6", node6);
        System.out.println(cache.get("key3"));
        System.out.println(cache.get("key2"));
        System.out.println(cache.get("key6"));
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Top_雨夜聆风丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值