Possible Duplicate:
What are the reasons why Map.get(Object key) is not (fully) generic
Why do we have contains(Object o) instead of contains(E e)?
As you all can see here, a templated java.util.List of type E has its contains
method not templated: it takes an Object
instead. Does anyone know why?
in what case would a List<String>
return true in myList.contains(new OtherNonString())
? If I'm not mistaken, never, unless the object that it's compared to has type E as an ancestor (which in my string example is impossible due to String
being final)
Is it only to maintain backwards compatibility with pre-generics versions? am I missing a use-case where it makes sense? if it's just for backwards compatibility, why not deprecate contains(Object)
and create a contains(E)
?
Edit:
Some of my sub-questions had been answered before. For reference, also check this question
if it's just for backwards compatibility, why not deprecate contains(Object) and create a contains(E)?
Because contains(Object)
and contains(E)
have the same type erasure (as you can see in this code sample) and hence would cause compilation errors. Also, deprecating methods was not an option, the top priority back then was to make legacy code work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With