In my application i have to use Collections but can suggest when to use for what scenario? Fast Retrival of data from a huge collection is the biggest criteria so which collection Class shall I use Regards, aakash
Here's my Collections Crib Sheet that I use to figure out which collection to use. A collection is just a place to put a bunch of things, maybe all the same class, maybe not. Any time you could have one or more of something, a collection may be just the ticket. Say you wanted to keep track of all the courses you have taken and calculate a GPA. You could load a bunch of Course objects from a file or database and add them to a collection, then go through the collection and get the grades. Hope that helped. Holler for more if not!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Thanks for sharing your "Crib Sheet". What type of collection to use where is an ongoing question for me and this will help sort things out. I do have a follow on question: Should I avoid using Hashtables and Vectors? If so, why? Is it a performance or maintainability issue?
Hashtables are no problem, I use them all the time. They are a little bit slower than HashMaps but old habits die hard Vectors ARE slow. Using an ArrayList instead can give you retrieval and storage times that can be 10x faster or more as compared to Vectors (I've tested this, a Servlet needed 200ms to run when using Vectors (it used Vectors for internal storage of data retrieved from a database, think 5 or so Vectors of maybe a hundred entries each), simply replacing those with ArrayLists dropped that to under 20ms. Those 20ms were mostly database access times).
Hashtables and Vectors are the legacy collections and should not be used, -- if you do, you may be stuck with the old/inefficient implementation forever. HashMaps and ArrayLists, on the other hand, would allow your code to become better along with the evolution of Java, and to use all the benefits of code reuse and mutation.