• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections and Generics  RSS feed

 
Ra Sank
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me the difference between collections and generics?
I have confused with 1.4 and 1.5 collection declarations like this!

1.4

ArrayList al;

1.5

ArrayList<String> al;

Where Generic making difference on collections on 1.5
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont have it to hand, but I would read the Sun tutorial on Collections and genrics, they are very good.
[ June 12, 2008: Message edited by: Gavin Tranter ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is.

The big difference between Collections in 1.4 and 1.5 is now you can specify the type of object your Collection can contain.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic example:
Before version 1.5, you could do this:


Now, after version 1.5, if you declare and instantiate your ArrayList as


You can only add String to the ArrayList.
Besides other thigs, it helps to avoid ClassCastException when you're reading the elements of the collection. Now you know what is the type of the elements.
 
Ra Sank
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Team,

Then can i say like this...

Generics are enhanced collections which will supports or restricts each collection with a specific type.
 
madura shantha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
collection is a data structure like arrays.
generics make collections type safe!

when you say 'ArrayList al',
the al is an ArrayList , and like that, ArrayList is a Collection.
in this code no generic applied.
so, you can insesrt anything(String,Integer,or any customer object) to this ArrayList.

but in the other form,
when you say, 'ArrayList<String> al',
you can only insert Strings to it.No Integers and anything else.
here,"<String>" is the generic type.
"ArrayList" is the collection.
 
Ra Sank
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I've Understood.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!