Argument Types of the Java Collection LinkListList Function

Possible duplicate:
Why doesn’t Java Collection remove common methods?

I noticed that some of the LinkedList operations take the generic type of the parameter E, and some take "Object" as the parameter. For instance,

add(E e) 
remove(Object o) 

Is there a specific reason for this? Why not “delete” to take the generic type E. (I know that it doesn't matter after the type is erased, but just interesting).

+5
source share
3 answers

Collection. , , . , API , . , contains .

0

, equals(), equals() Object .

+1

Since the caller can call removeeither containswith any type of object, and the code will still execute just fine. A compile-time contract that uses the collection type parameter simply guarantees * that everything in the collection will be of the given type. It doesn't matter if you ask if it contains an object of another type.

* Nitpicker Corner, I know that Java generics really are not a guarantee. I suppose more syntactic sugar.

0
source

All Articles