【问题标题】:Object reference equals check for LRU Cache implementation对象引用等于检查 LRU 缓存实现
【发布时间】:2021-05-18 22:11:57
【问题描述】:

我对Java对equals的理解是,如果两个对象有相同的引用,那么它们通过==相等。例如,如果是字符串,则需要使用 .equals() 来比较值;

下面的代码是一个示例 LRUCache。在 addToEnd 方法中,有一次我将尾部与节点(它说什么都不做)进行比较,即使它们具有相同的引用,它们也不会命中该代码块。有人可以解释为什么吗?调试时,它们具有完全相同的引用。

public class LRUCache {

    private class Node {
        private int key;
        private int value;
        private Node prev;
        private Node next;

        private Node (int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private Map<Integer, Node> lookup;
    private int capacity;
    private int size;
    private Node head;
    private Node tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.lookup = new HashMap<Integer, Node>();
    }

    private void addToEnd(Node node) {
        if (node == this.tail) {
            //do nothing
        }
        if (this.tail != null) {
            if (node == this.head) {
                this.head = this.head.next;
            }
            Node prev = node.prev;
            Node next = node.next;
            if (prev != null) {
                prev.next = next;
            }

            if (next != null) {
                next.prev = prev;
            }
            this.tail.next = node;
            node.prev = this.tail;
            node.next = null;
            this.tail = node;
        } else {
            this.head = node;
            this.tail = this.head;
        }
    }

    private void popHead() {
        if (this.head != null) {
            this.lookup.remove(this.head.key);
            Node node = this.head.next;
            this.head = node;
            this.size--;
        }
    }

    public int get(int key) {
        if (lookup.containsKey(key)) {
            Node node = lookup.get(key);
            addToEnd(node);
            return node.value;
        } else {
            return -1;
        }
    }

    public void put(int key, int value) {
        if (lookup.containsKey(key)) {
            Node node = lookup.get(key);
            addToEnd(node);
            node.value = value;
            return;
        } else if (this.size == this.capacity) {
            popHead();
        }
        Node newNode = new Node(key, value);
        addToEnd(newNode);
        this.lookup.put(key, newNode);
        this.size++;
    }
}

这是我运行的代码:

       LRUCache lRUCache = new LRUCache(1);
       lRUCache.put(2, 1);
       lRUCache.get(2); 
       lRUCache.put(3, 2); 
       System.out.println(lRUCache.get(2)); //getting 1 but should be -1
       System.out.println(lRUCache.get(3));

【问题讨论】:

    标签: java object hashmap equals lru


    【解决方案1】:
            if (node == this.tail) {
                //do nothing
            }
    

    本意可能是什么都不做,然后返回

    return 关键字丢失。

    所以,添加 return 应该会给出正确的行为

            if (node == this.tail) {
                return;
            }
    

    【讨论】:

    • 感谢您的帮助!我使用了带返回的调试器,它成功了。
    猜你喜欢
    • 2015-02-13
    • 2021-03-05
    • 2010-11-03
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多