Java collections. add(E obj), but remove(Object obj) [duplicate]

一个人想着一个人 提交于 2019-12-01 07:54:16

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8373838/java-collections-adde-obj-but-removeobject-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!