Java 集合。添加(E obj),但删除(Object obj)
可能的重复:
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是通用前时代遗留下来的。
add
和remove
方法都用于接受Object
参数。当引入泛型时,有充分的理由更改add
方法,但确实没有任何充分的理由更改remove
方法(因为您引用的原因允许保持原样)。我的猜测是,如果今天从头开始设计,
Collection.remove
将采用通用参数,而不是匿名对象。I think it's left over from pre-generic days. Both the
add
andremove
methods used to take anObject
argument. When generics were introduced, there was good reason to change theadd
method, but there really wasn't any good reason to change theremove
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.