Java 集合。添加(E obj),但删除(Object obj)

发布于 2024-12-19 10:36:41 字数 461 浏览 2 评论 0原文

可能的重复:
为什么 Java 集合的删除方法不是通用的?

我有一个问题关于Collection中声明的java函数的签名。 问题是:为什么add的签名涉及E(我们的类型),而remove有参数Object?

我在WWW上看到过这个问题的回答,但我不确定原因

“在删除中我们只需要 1 个操作:等于,而 Object 提供了它”

是合理的。

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

I have a question about signature of java functions declared in Collection.
The question is: why signature of add involves E (our type) but remove has parameter Object?

I have seen one response in WWW of this question, but I'm not sure that reason

"in remove we only need 1 operation: equals and Object provides it"

is plausible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-12-26 10:36:41

我认为这是通用前时代遗留下来的。 addremove 方法都用于接受 Object 参数。当引入泛型时,有充分的理由更改 add 方法,但确实没有任何充分的理由更改 remove 方法(因为您引用的原因允许保持原样)。

我的猜测是,如果今天从头开始设计,Collection.remove 将采用通用参数,而不是匿名对象。

I think it's left over from pre-generic days. Both the add and remove methods used to take an Object argument. When generics were introduced, there was good reason to change the add method, but there really wasn't any good reason to change the remove method (because the reason you cited allowed it to remain as is).

My guess is that if it were designed from scratch today, Collection.remove would take a generic argument, not an anonymous Object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文