LinkedList removeLastOccurrence

LinkedList removeLastOccurrence

我是计算机科学的新手,所以请原谅我的无知。对于 LinkedList that is in JavaremoveLastOccurrence方法是在传入的对象之间使用equals方法还是只是比较内存位置?

根据 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/LinkedList.java,该方法的来源是:

public boolean removeLastOccurrence(Object o) {
    if (o == null) {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}

因此,如果您要删除的对象是 null,则它会在列表中查找 null 值。如果对象不是 null,则它使用 equals 方法。但是,请记住,除非 equals 被专门覆盖以实现值相等,否则它将默认为身份相等(即,通过比较内存地址,如 == 对对象所做的那样)