Java 当您在数组列表上调用 remove(object o) 时,它如何比较对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2894618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When you call remove(object o) on an arraylist, how does it compare objects?
提问by Thomas Winsnes
When you call remove(object o) on an arraylist in java, how does it compare the objects to find the correct one to remove? does it use the pointer? or does it compare the objects using the interface Comparable?
当您在 java 中的数组列表上调用 remove(object o) 时,它如何比较对象以找到要删除的正确对象?它使用指针吗?还是使用接口 Comparable 比较对象?
采纳答案by Eric
ArrayList
remove()
relies on the objects implementation of the Equal
method. If no implementation has been done then the object is removed by Object
's implementation of Equals
which indeed is the pointer comparison.
ArrayList
remove()
依赖于方法的对象实现Equal
。如果没有实现,那么对象会被Object
的实现移除,Equals
它的实现确实是指针比较。
From the documentation on ArrayList
-
从文档中ArrayList
-
More formally, removes the element with the lowest index i such that
(o==null ? get(i)==null : o.equals(get(i)))
(if such an element exists)
更正式地,删除具有最低索引 i 的元素,使得
(o==null ? get(i)==null : o.equals(get(i)))
(如果存在这样的元素)
Object equal
method documentation -
对象equal
方法文档 -
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values
x
andy
, this method returns true if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).
Object 类的 equals 方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值
x
andy
,当且仅当x
andy
引用同一个对象(x == y
具有值true
)时,此方法返回真。
回答by leonbloy
The docsanswer your question:
该文档回答你的问题:
Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element
e
such that(o==null ? e==null : o.equals(e))
, if the collection contains one or more such elements.
从此集合中移除指定元素的单个实例(如果存在)(可选操作)。更正式地,如果集合包含一个或多个这样的元素,则删除一个元素
e
使得(o==null ? e==null : o.equals(e))
。
回答by YGL
It uses equals()
它用 equals()
from the docs:
从文档:
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
从此列表中删除第一次出现的指定元素(如果存在)。如果列表不包含该元素,则它保持不变。更正式地,删除具有最低索引 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果这样的元素存在)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true。
回答by polygenelubricants
You should always consult the API for this kind of information.
您应该始终咨询 API 以获取此类信息。
ArrayList.remove(Object o)
: Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest indexi
such that(o==null ? get(i)==null : o.equals(get(i)))
(if such an element exists).
ArrayList.remove(Object o)
: 从此列表中删除第一次出现的指定元素(如果存在)。如果列表不包含该元素,则它保持不变。更正式地,删除具有最低索引的元素,i
使得(o==null ? get(i)==null : o.equals(get(i)))
(如果存在这样的元素)。
Perhaps you were confusing this with e.g. TreeSet
:
也许您将此与例如混淆TreeSet
:
java.util.TreeSet
: Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equalsif it is to correctly implement theSet
interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because theSet
interface is defined in terms of theequals
operation, but aTreeSet
instance performs all element comparisons using itscompareTo
(orcompare
) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.
java.util.TreeSet
: 注意集合维护的排序(无论是否提供显式比较器)必须与equals一致,才能正确实现Set
接口。(请参阅 Comparable 或 Comparator 以获取与 equals 一致的精确定义。)这是因为Set
接口是根据equals
操作定义的,但TreeSet
实例使用其compareTo
(orcompare
) 方法执行所有元素比较,因此两个元素被视为相等通过这种方法,从集合的角度来看,是相等的。
(Unfortunately e.g. TreeSet.remove
method itself doesn't have any explicit reminder of the above caveat, but at least it's prominently placed at the top of the class documentation)
(不幸的是,例如TreeSet.remove
方法本身没有对上述警告的任何明确提醒,但至少它被显着地放置在类文档的顶部)
An illustrative example
一个说明性的例子
The following snippet illustrates the difference in behaviors between collections that use equals
(such as an ArrayList
) and collections that use compare/compareTo
(such as a TreeSet
).
以下代码段说明了使用equals
(例如ArrayList
)的集合和使用compare/compareTo
(例如)的集合之间的行为差异TreeSet
。
import java.util.*;
public class CollectionEqualsCompareTo {
static void test(Collection<Object> col, Object o) {
col.clear();
col.add(o);
System.out.printf("%b %b %b %b%n",
col.contains(o),
col.remove(o),
col.contains(o),
col.isEmpty()
);
}
public static void main(String[] args) {
Object broken1 = new Comparable<Object>() {
// Contract violations!!! Only used for illustration!
@Override public boolean equals(Object o) { return true; }
@Override public int compareTo(Object other) { return -1; }
};
Object broken2 = new Comparable<Object>() {
// Contract violations!!! Only used for illustration!
@Override public boolean equals(Object o) { return false; }
@Override public int compareTo(Object other) { return 0; }
};
test(new ArrayList<Object>(), broken1); // true true false true
test(new TreeSet<Object>(), broken1); // false false false false
test(new ArrayList<Object>(), broken2); // false false false false
test(new TreeSet<Object>(), broken2); // true true false true
}
}