问题
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