Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection<String>

Tags:

java

generics

Whats the main difference between a Collection of String (Collection)and a simple, plain collection?

like image 683
Global Dictator Avatar asked Mar 20 '26 19:03

Global Dictator


2 Answers

A plain Collection will accept any kind of Object, while a Collection<String> will only accept Strings.

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.

like image 176
Bill the Lizard Avatar answered Mar 23 '26 09:03

Bill the Lizard


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.

like image 20
Vinze Avatar answered Mar 23 '26 07:03

Vinze