LRUCache:双向链表+Map

import java.util.HashMap;
import java.util.Map;

public class LRUCache<K, V> {

    public static void main(String[] args) {
        // 1 -1 -1 3 4
        test1();
        System.out.println("-------------------------");
        // 1 -1 2
        test2();
        System.out.println("-------------------------");
        // 2 -1
        test3();
    }

    public static void test1() {
        LRUCache<Integer, Integer> lruCache = new LRUCache<Integer, Integer>(2, -1);

        lruCache.put(1, 1);
        lruCache.put(2, 2);
        System.out.println(lruCache.get(1));
        lruCache.put(3, 3);
        System.out.println(lruCache.get(2));

        lruCache.put(4, 4);
        System.out.println(lruCache.get(1));
        System.out.println(lruCache.get(3));
        System.out.println(lruCache.get(4));
    }

    public static void test2() {
        LRUCache<Integer, Integer> lruCache = new LRUCache<Integer, Integer>(1, -1);

        lruCache.put(2, 1);
        System.out.println(lruCache.get(2));
        lruCache.put(3, 2);
        System.out.println(lruCache.get(2));
        System.out.println(lruCache.get(3));
    }

    public static void test3() {
        LRUCache<Integer, Integer> lruCache = new LRUCache<Integer, Integer>(2, -1);

        lruCache.put(2, 1);
        lruCache.put(2, 2);
        System.out.println(lruCache.get(2));
        lruCache.put(1, 1);
        lruCache.put(4, 1);
        System.out.println(lruCache.get(2));
    }

    public LRUCache(int capacity) {
        this(capacity,null);
    }

    public LRUCache(int capacity, V defaultValue) {
        this.capacity = capacity;
        this.defaultValue = defaultValue;
        head = new Node<>();
        tail = new Node<>();
        head.next = tail;
        tail.prev = head;
    }

    private Map<K, Node<K, V>> cache = new HashMap<>();

    private int capacity;

    private int size;

    private V defaultValue;

    private Node<K, V> head, tail;

    public V get(K k) {
        Node<K, V> node = cache.get(k);
        if (node == null) {
            return defaultValue;
        } else {
            moveToHead(node);
            return node.v;
        }
    }

    public void put(K k, V v) {
        Node<K, V> node = cache.get(k);
        if (node == null) {
            node = new Node<>(k, v);
            cache.put(k, node);
            addToHead(node);
            size++;
            if (size > capacity) {
                removeTail();
                size--;
            }
        } else {
            node.v = v;
            moveToHead(node);
        }
    }

    private void addToHead(Node node) {
        node.prev = head;
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
    }

    private void removeTail() {
        Node<K, V> last = tail.prev;
        last.prev.next = last.next;
        last.next.prev = last.prev;
        cache.remove(last.k);
    }

    private void moveToHead(Node<K, V> node) {

        Node<K, V> first = head.next;
        if (first == node) {
            return;
        }

        Node prev = node.prev;
        Node next = node.next;
        if (prev != null) {
            prev.next = next;
        }
        if (next != null) {
            next.prev = prev;
        }
        node.prev = first.prev;
        node.next = first;
        first.prev = node;
        head.next = node;

    }

    private class Node<K, V> {
        K k;
        V v;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
        }

        public Node(K k, V v) {
            this.k = k;
            this.v = v;
        }

        public Node(K k, V v, Node<K, V> prev, Node<K, V> next) {
            this.k = k;
            this.v = v;
            this.prev = prev;
            this.next = next;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值