<分区>
Possible Duplicate:
Why aren't Java Collections remove methods generic?
有谁知道为什么MyDamnedUnclearList.remove()
方法采取Object element
而不是 T element
(或 V extends T element
)?
我花了两个小时才找到混合两者的错误 int
和 Integer
Java 类型(在 List<Thread>
列表中...
<分区>
Possible Duplicate:
Why aren't Java Collections remove methods generic?
有谁知道为什么MyDamnedUnclearList.remove()
方法采取Object element
而不是 T element
(或 V extends T element
)?
我花了两个小时才找到混合两者的错误 int
和 Integer
Java 类型(在 List<Thread>
列表中...
最佳答案
这个问题与现有代码和规范的向后兼容性有关。
What are the reasons why Map.get(Object key) is not (fully) generic
一切都在 Javadoc 的细节中:
boolean remove(Object o)
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 this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
因此您会发现,对象是否具有相同类型并不重要,只要它们相等即可。
关于java - 为什么列表的 remove() 方法采用 Object 而不是 T?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199951/