Sunday, December 4, 2011

Making decision for picking right Java Collection

How to choose which Java collection class to use?

The Java Collections API provides a whole host of data structures, especially since the API was expanded in Java 5 (and again slightly in Java 6) to include concurrent collections. At first, the array of choices can be a little daunting: should I use a HashMap or a LinkedHashMap? When should I use a list or a HashSet? When should I use a TreeMap rather than a HashMap? But with a bit of guidance, the choice needn't be quite so daunting. There are also a few cases where it's difficult to decide because the choice is very arguable. And in other cases, having a clear set of rules of thumb can guide you to an appropriate decision.

Basic approach to choosing a collection

The overall approach I'd suggest for choosing is as follows:

choose the general type of organisation that your data needs to have (e.g. map or list); without too much thought, this is usually fairly clear;
then, choose the implementation of that type that has the minimum functionality that you actually require (e.g. don't choose a sorted structure if you don't actually need the data to be sorted).
In general, the algorithm that underlies each collection class is designed to be a good tradeoff between efficiency and certain minimal requirements. So as long as you understand the minimal requirements that a given class is designed to provide, you shouldn't need to get too bogged down in the actual algorithms (though if you are interested in algorithms, the source code to all the collections classes is available and they make fascinating case studies, of course).

The following table/links here(Javamex.com) gives you further insight into details of making right decision -

1. Reading 1
2. Reading 2

No comments:

Post a Comment