Whats the main difference between a Collection of String (Collection)and a simple, plain collection?
-
See also http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – polygenelubricants Jul 07 '10 at 11:39
4 Answers
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.

- 398,270
- 210
- 566
- 880
-
@Koen: Thanks. I forgot that the WMD parser cancels those out unless you put it in code spans. – Bill the Lizard Jul 07 '10 at 11:36
-
Thanks for the notification. I thought that same first when I saw my post and then realised the obvious. – Global Dictator Jul 07 '10 at 12:03
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.

- 2,549
- 3
- 22
- 23
collection is generics, and it is used to define which type of data it is going to handle, precautions for runtime error, its better it tells you at compile time. so simple collection can accept any thing but collection of string will only accept string.
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.

- 11,070
- 14
- 66
- 115