Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<E>.contains(Object), why? [duplicate]

Tags:

java

generics

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

like image 795
Hilikus Avatar asked Nov 09 '12 16:11

Hilikus


1 Answers

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.

like image 141
Sean Patrick Floyd Avatar answered Oct 05 '22 10:10

Sean Patrick Floyd