java - 为什么列表的 remove() 方法采用 Object 而不是 T?

标签 java api list

<分区>

Possible Duplicate:
Why aren't Java Collections remove methods generic?

有谁知道为什么MyDamnedUnclearList.remove()方法采取Object element而不是 T element (或 V extends T element)?

我花了两个小时才找到混合两者的错误 intInteger 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/

相关文章:

list - Scala 反转 zip

java - 没有任何泛型的 kotlin 错误的可空性推断

java - 如何停用 SSL 验证?

java - 在 O(n) 而不是 O(nlogn) 中重新平衡 BST

javascript - 如何在 JS 和 PHP API 之间设置 session token ?

javascript - 异步 javascript promise 使用 settimeout 调用

c# - 为什么 List<T> 的 SequenceEqual 返回 false?

java - 如何在 Janino 脚本中表示值列表

java - 将 JSP 部署到用户,如 setup.exe 或 .dmg 文件

java - 如何创建具有可滚动内容的 JTable 单元格