Whats the main difference between a Collection of String (Collection)and a simple, plain collection?
A plain Collection
will accept any kind of Object
, while a Collection<String>
will only accept String
s.
If instead of String
you had a Collection
of something that could be extended, say Collection<List>
, then the collection would accept anything that is a subtype of List
. Sun's Java Tutorial on Generics is a good place to go to learn more.
Once compiled, nothing... But it can help you not to try to put any other object than a String while coding, and you don't have to cast back the objects that you retrieve from the collection.
Generics. using a Collection as an argument or as a return type ensures that the caller/accessor to anticipate the objects to be present in the collection. this helps to avoid unnecessary casting to String.
Type erasure ensures that the generic definition is removed at runtime.A feature which helps in backward compalitibility.