• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Collections and Generics

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. I've Understood.
 
reply
    Bookmark Topic Watch Topic
  • New Topic