使用Java实现LRU缓存和LFU缓存

LRU缓存

问题描述

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

代码实现

class LRUCache {
    int size;
    int capacity;
    Map<Integer, DoubleListNode> map;
    DoubleListNode head;
    DoubleListNode tail;

    public LRUCache(int capacity) {
        size = 0;
        this.capacity = capacity;
        map = new HashMap<>();
        head = new DoubleListNode(0, 0);
        tail = new DoubleListNode(0, 0);
        head.next = tail;
        tail.pre = head;
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        DoubleListNode node = map.get(key);
        remove(node);
        addToHead(node);
        return node.val;
    }

    public void put(int key, int value) {
        if(map.containsKey(key)){
            DoubleListNode node = map.get(key);
            remove(node);
            node.val=value;
            addToHead(node);
        }else{
            if (size < capacity) {
                DoubleListNode node = new DoubleListNode(key, value);
                addToHead(node);
                map.put(key, node);
                size++;
            } else {
                DoubleListNode pre = tail.pre;
                int k = pre.key;
                map.remove(k);
                removeFromTail();
                DoubleListNode node = new DoubleListNode(key, value);
                addToHead(node);
                map.put(key, node);
            }
        }

    }

    public void remove(DoubleListNode node) {
        node.pre.next = node.next;
        node.next.pre = node.pre;
    }

    public void addToHead(DoubleListNode node) {
        DoubleListNode next = head.next;
        next.pre = node;
        node.next = next;
        head.next = node;
        node.pre = head;
    }

    public void removeFromTail() {
        remove(tail.pre);
    }
}

class DoubleListNode {
    public int key;
    public int val;
    public DoubleListNode pre;
    public DoubleListNode next;

    public DoubleListNode() {
    }

    public DoubleListNode(int key, int val) {
        this.key = key;
        this.val = val;
    }
}

LFU缓存

问题描述

请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。
实现 LFUCache 类:
LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象
int get(int key) - 如果键 key 存在于缓存中,则获取键的值,否则返回 -1 。
void put(int key, int value) - 如果键 key 已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量 capacity 时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最久未使用 的键。
为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。
当一个键首次插入到缓存中时,它的使用计数器被设置为 1 (由于 put 操作)。对缓存中的键执行 get 或 put 操作,使用计数器的值将会递增。

代码实现

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

class LFUCache {
    int time;
    int capacity;
    Map<Integer, Node> map;
    TreeSet<Node> treeSet;

    public LFUCache(int capacity) {
        this.capacity = capacity;
        this.time = 0;
        map = new HashMap<>();
        treeSet = new TreeSet<>();
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        Node node = map.get(key);
        treeSet.remove(node);
        node.count += 1;
        node.time = ++time;
        treeSet.add(node);
        map.put(key, node);
        return node.value;
    }

    public void put(int key, int value) {
        if(!map.containsKey(key)) {
            if (map.size() == capacity) {
                Node node = treeSet.first();
                map.remove(node.key);
                treeSet.remove(node);
            }
            Node newNode = new Node(1,++time,key, value);
            map.put(key, newNode);
            treeSet.add(newNode);
        }else{
            Node node = map.get(key);
            treeSet.remove(node);
            node.count += 1;
            node.time = ++time;
            node.value = value;
            treeSet.add(node);
            map.put(key, node);
        }

    }
}

class Node implements Comparable<Node> {
    int count;
    int time;
    int key;
    int value;

    public Node() {

    }

    public Node(int count, int time, int key, int value) {
        this.count = count;
        this.time = time;
        this.key = key;
        this.value = value;
    }

    @Override
    public int compareTo(Node o) {
        return this.count==o.count?this.time-o.time:this.count-o.count;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Node node = (Node) o;
        return count == node.count && time == node.time;
    }

    @Override
    public int hashCode() {
        return 10000007 * count + time;
    }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今天不coding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值